javascript运动功能-分享到

时间:2022-04-29
本文章向大家介绍javascript运动功能-分享到,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
<script>
  //窗体载入,为div控件绑定事件
        window.onload = function () {

            var div1 = document.getElementById('div1');
            //mouseover
            div1.onmouseover = function () {
                startMove(0);
            }
            //mouseout
            div1.onmouseout = function () {

                startMove(-100);
            }
        }
        //定义定时器
        var timer = null;
        //淡入和淡出
        function startMove(targetPoint) {

       
            clearInterval(timer);
            var div1 = document.getElementById('div1');
          
            timer = setInterval(function () {

                var ispeed = 0;
               if (div1.offsetLeft < targetPoint) {

                   ispeed = 10;
               }
               else {

                   ispeed = -10;
               }
                //控件位置
               if (div1.offsetLeft == targetPoint) {
                    clearInterval(timer);
                }
                else {

                    div1.style.left = div1.offsetLeft + ispeed + "px";
                }
            },30);
           
        }
</script>