javascript - 求解:为什么这段代码中输出结果次数呈现二的n次方次 (请看代码)
巴扎黑
巴扎黑 2017-04-10 17:15:58
[JavaScript讨论组]

<html>
<head>

<script src="../jquery-2.1.1.min.js"></script> //2.11版本
<style>

 .box1 {
        position: relative;
        top: 0px;
        left: 0px;
        width: 100px;
        height: 100px;
        background: red;
        transition: all 0.5s ease;
    }
    
  • {

    margin: 0;
    padding: 0;

    }

  1. {

    width: 100%;
    height: 10%;

    }

  2. {

    width: 100%;
    height: 100%;

    }
    </style>

</head>

<body>

<p class="box1">

</p>

<script>

    var totalFun = null;
    $(document).on("mousemove", totalFun = function () {
        showClientPos(event);
        move();
        bindEvent();
        $(document).off("mousemove");
    });

    function showClientPos(e) {
        var posArr = [];
        posArr["x"] = e.clientX;
        posArr["y"] = e.clientY;
        return posArr;
    }

    function move() {
        var objX = showClientPos(event).x + "px";
        var objY = showClientPos(event).y + "px";
        $(".box1").css("transform", "translate(" + objX + "," + objY + ")");
       console.log(objX);
    }
    function bindEvent() {
        var transEndEventName = whichTransitionEvent();
        $(".box1").bind(transEndEventName, function () {
            $(document).on("mousemove", totalFun)
        });
    }

    function whichTransitionEvent() {

        var t;

        var el = document.createElement('fakeelement');

        var transitions = {

            'transition': 'transitionend',

            'OTransition': 'oTransitionEnd',

            'MozTransition': 'transitionend',

            'WebkitTransition': 'webkitTransitionEnd',

            'MsTransition': 'msTransitionEnd'

        }
        for (t in transitions) {

            if (el.style[t] !== undefined) {

                return transitions[t];

            }

        }

    }
</script>

</body>
</html>

巴扎黑
巴扎黑

全部回复(1)
PHP中文网
$(document).on("mousemove", totalFun = function () {
    ...
    move();
    bindEvent();
    ...
});

每次mousemove事件都会调用一次move,同时调用一次bindEventcss变换结束时又绑定了一次mousemove事件。所以每次调用的mousemove都会翻倍。

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

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