javascript - js图片淡入淡出时图片要跳一下,怎么解决
阿神
阿神 2017-04-10 15:54:05
[JavaScript讨论组]
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        #p1{
            width: 200px;
            height: 200px;
            background-color: blue;
            opacity: 0.3;
        }
    </style>
    <script>

        window.onload=function(){
            var myp=document.getElementById('p1');
            myp.onmouseover=function(){
                startmove(0.9);
            }
            myp.onmouseout=function(){
                startmove(0.3);
            }
        }
        var timer=null;
        
        function startmove(itarget){
            var speed=0;
            var tmd=0;
            var myp=document.getElementById('p1');
            clearInterval(timer);
            timer=setInterval(function(){
                
                if(tmd<itarget)
                {
                    speed=0.01;
                }
                else
                {
                    speed=-0.01;
                }
                if(tmd==itarget)
                {
                    clearInterval(timer);
                }
                else
                {
                    tmd+=speed;
                    myp.style.opacity=tmd;
                }    
            }, 30);
        }
    </script>
</head>
<body>
    <p id="p1">
        
    </p>
</body>
</html>
阿神
阿神

闭关修行中......

全部回复(1)
PHP中文网

因为你已经给tmd赋值为0了,每次执行函数,tmd都是从0开始算起的。

var op=document.querySelector('#foo'),
    timer=null;

function startMove(obj,target){
    var timer=null,
        tmd=getComputedStyle(obj,null).opacity,//就是这里
        speed=0;
    tmd=parseFloat(tmd)
    obj.addEventListener('mouseover',function(){
        clearInterval(timer);
        timer=setInterval(function(){
            if(tmd<target){
                speed=0.01;
                tmd+=speed;
                obj.style.opacity=tmd
            }else{
                clearInterval(timer)
            }
        },10)
    },false)
}

startMove(op,1)

试试我这个,只有移入部分,移出同理
链接描述

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

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