【问题标题】:jQuery mouseover, animate left or right while truejQuery 鼠标悬停,为真时向左或向右动画
【发布时间】:2016-08-23 14:34:52
【问题描述】:

我想根据用户的鼠标位置向右或向左移动溢出的 div(根据窗口负载计算)。 (鼠标还在那里时的连续动画)。我有一些错误/问题。

  1. 在鼠标悬停时不停地向该方向移动。
  2. 如果我使用 animate 函数,这将与 start 和 stop 一起工作,一遍又一遍会导致动画效果不佳。

有什么想法吗?

注意:我不想使用除 jquery 之外的任何额外库。

$(window).load(function() {
    var buildingsWrapper = $('#buildings'),
        lastBuilding = $('.building:last');

    buildingsWrapper.width(parseInt(lastBuilding.css('left')) + lastBuilding.width());

    var followMouseMove = function() {
        var animStarted = false;

        // ok now, mouse over but this will work for just one time.
        buildingsWrapper.on('mouseover', function(e) {
            if(e.clientX >= $(window).width() - 100) {
                var left = buildingsWrapper.css('left');

                if(!animStarted) {
                    animStarted = true;

                    // will work but will stop and start again after animStarted set to false. that start / stop is not what i want.
                    buildingsWrapper.animate({
                        left: parseInt(left) - 50
                    }, 300, function() {
                        animStarted = false;
                    });
                }
            }
        });
    };

    followMouseMove();
});

【问题讨论】:

    标签: javascript jquery html animation


    【解决方案1】:

    试试鼠标移动:

    buildingsWrapper.on('mouseover', function(){
         $(document).mousemove( function(e) {
                var mouseX = e.pageX;
                var slideT = (mouseX*(-1))+"px";
    
    
    
            buildingsWrapper.css({'left': slideT});
    });     
            });
    

    尝试修改slideT得到你想要的动画

    【讨论】:

    • 无法工作,因为用户必须移动鼠标来制作动画(向右或向左)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 2011-04-07
    • 2012-05-04
    • 1970-01-01
    相关资源
    最近更新 更多