jquery - 我想在每个 li 悬停时从左到右为 li 元素设置动画

标签 jquery html css animation

    <!Doctype html>
<html>
    <head>
        <link href="style.css" rel="stylesheet" type="text/css" />
        <script src="jquery-1.10.2.min.js" type="text/javascript"></script>
        <script src="animation.js" type="text/javascript"></script>
    </head>
    <body>
        <ul class="svertical">
            <li><a>MenuElement1</a></li>
            <li><a>MenuElement2</a></li>
            <li><a>MenuElement3</a></li>
            <li><a>MenuElement4</a></li>
            <li><a>MenuElement5</a></li>
            <li><a>MenuElement6</a></li>        
        </ul>

    </body>
</html>`

` CSS添加动画 css 来显示 li 元素,我创建了我的菜单项 CSS 来添加动画 css 来显示 li 元素,我创建了我的菜单项 CSS 来添加动画 css 显示我创建的菜单项中的 li 项

ul.svertical{
width: 200px; /* width of menu */
overflow: auto;
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#eeeeee), color-stop(100%,#cccccc)) no-repeat; /* background of menu */
margin:0px;
padding: 0;
padding-top: 7px; /* top padding */
list-style-type: none;
position:absolute;
}

ul.svertical li{
text-align: left; /* right align menu links */
position: relative;
display: inline-block;
text-indent: 5px;
overflow: hidden;
background: rgb(127, 201, 68); /* initial background color of links */
font: bold 16px Germand;
text-decoration: none;
padding: 5px;
color: black;
border:1px solid;
border-radius:5px;
left:-110px;
}

animation.js

$(document).ready( function(){
    var timer = 0;
    $('.svertical li').hover( 
    function(){
        $(this).stop().animate({ left: "0px" }, 'fast');
        $('.svertical li').each(function (i){
            timer = (timer * .8 + 150);
            $(this).stop().animate({ left: "0px" }, timer);

        });
    },
    function(){
        $(this).stop().animate({ left: "-110px" }, 'fast');
        $('.svertical li').each(function (i){
            timer = (timer * .8 + 150);
            $(this).stop().animate({ left: "-110px" }, timer);

        });
    });
});

最佳答案

看来你只需要重置你的计时器变量:

$(document).ready( function(){
    var timer = 0;
    $('.svertical li').hover( 
    function(){
        timer = 0; // Reset timer variable
        $(this).stop().animate({ left: "0px" }, 'fast');
        $('.svertical li').each(function (i){
            timer = (timer * .8 + 150);
            $(this).stop().animate({ left: "0px" }, timer);

        });
    },
    function(){
        $(this).stop().animate({ left: "-110px" }, 'fast');
        $('.svertical li').each(function (i){
            timer = (timer * .8 + 150);
            $(this).stop().animate({ left: "-110px" }, timer);

        });
    });
});

关于jquery - 我想在每个 li 悬停时从左到右为 li 元素设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22278709/

相关文章:

css - 指令容器的高度为 0px,宽度为 0px - angularjs

javascript - 如何在几秒钟后自动移动轮播的幻灯片?

php - 使用 jQuery 从 PHP 表单中选择文本输入

javascript - 如何运行 Geolocation watchPosition 直到获得准确的结果?

jquery - 使用Symfony,jQuery,FOSRestBundle和NelmioCorsBundle进行CORS

jQuery 动画 <object> 标签

html - IE8 中两个固定 div 元素之间的响应 div

html - 音频html标签无法快进或快退控制

javascript - 输入[类型文件]更改后,变量 imageLen 不会重置

css - 如何隐藏警告并仅在 Stylelint linting 结果中显示错误?