javascript - 侧边影藏div,以10px/1ms滑出div,以1px/1ms滑入的途中再次onmouseover,为什么div不受控制乱弹
伊谢尔伦
伊谢尔伦 2017-04-10 18:07:22
[JavaScript讨论组]

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

<title>匀速动画</title>
<style type="text/css">
    #move{
        width: 200px;
        height: 200px;
        background-color: #996666;
        position: absolute;
        top: 20px;
        left: -190px;
        cursor:pointer;
    }
</style>
<script type="text/javascript">

window.onload=function(){

        var getp = document.getElementById('move');

            getp.onmouseover = function(){
                startMove();
            }
            getp.onmouseout = function(){
                moveBack();
            }

              var timer = null;

             function startMove(){
                    clearInterval(timer);
            timer = setInterval(function(){
                 if(getp.offsetLeft==0){
                     clearInterval(timer);
                 }
                 else{ 
                     getp.style.left= getp.offsetLeft+10+"px";
                 }
             },1);

            }

            function moveBack(){    
                    clearInterval(timer);
            timer = setInterval(function(){
                 if(getp.offsetLeft==-190){
                     clearInterval(timer);
                 }
                 else{ 
                     getp.style.left= getp.offsetLeft-1+"px";
                 }
             },1);

            }
    }
</script>

</head>
<body>
<p id="move">

</p>
</body>
</html>

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(1)
巴扎黑

这么简单的效果,直接用css写不是更好么
至于乱动,具体操作是先完全弹出来,然后p缓慢缩回去时再次把鼠标放在p上触发mouseover,是因为onmouseout中定时器的间隔比onmouseover定时器的移动距离小,要改的话,可以设置一个状态,等完全缩进去的时候onmouseover再允许定义定时器

function startMove() {
            clearInterval(timer);
            if(getp.offsetLeft != -190){
                return;
            }
            timer = setInterval(function() {
                if (getp.offsetLeft == 0) {
                    clearInterval(timer);
                } else {
                    getp.style.left = getp.offsetLeft + 10 + "px";
                }
            }, 1);

        }
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号