简体   繁体   English

jQuery:计算相对于$(window)的'margin-left'或'left'。scrollLeft()在Firefox中确实参差不齐-使用.animate()或.css()

[英]jquery: calculating 'margin-left' or 'left' relative to $(window).scrollLeft() is really jagged in Firefox — using .animate() or .css()

I have a horizontally scrolling website, and I have a block that I want to stay in frame at all times as the user scrolls right. 我有一个水平滚动的网站,并且我想在用户向右滚动时始终保持框住状态。 It looks perfectly smooth in webkit browsers, but is crazy jagged in Firefox and I don't really care about IEs. 它在webkit浏览器中看起来非常流畅,但在Firefox中却疯狂地锯齿了,我并不真正在意IE。

function fixMyId(){
   $('#myId').css({'margin-left': 150 + $(window).scrollLeft()});
}

function fixMyIdAlt(){
   $('#myId').stop().animate({'margin-left': 150 + $(window).scrollLeft()}, 300);
}

And then I have it triggered on window scroll. 然后在窗口滚动中触发它。

What would be a best way to average out the scrolling, so that maybe every so many seconds or pixels of scrolling it fires the function, or upon stopping the scrolling it animates the block into place? 求平均滚动效果的最佳方法是什么,这样可能每隔几秒钟或滚动像素就会触发该功能,或者在停止滚动时使该块动画到位? I tried playing with delay() but that doesn't do anything. 我试着用delay()玩,但是那什么也没做。 And this one just looks stupid (plus I have no idea what the overhead of this kind of crunching is): 而且这看起来很愚蠢(再加上我不知道这种处理的开销是多少):

function fixMyIdStupid(){
    window.scrollCounter++;
    if(window.scrollCounter % 20 == 0) $('#myId').stop().animate({'margin-left': 150 + $(window).scrollLeft()}, 300);
}

So what do I do? 那我该怎么办? setTimeout and setInterval may be required, but those always make my head hurt. setTimeout和setInterval可能是必需的,但这些总是使我头受伤。

EDIT: Here's a jsfiddle of it in action: http://jsfiddle.net/xsxSq/ 编辑: 这是一个实际的jsfiddle: http : //jsfiddle.net/xsxSq/

The #f0f square is the #myId. #f0f正方形是#myId。

I tried to do such things as well, problem is that the scroll event isn't fired as much as you want. 我也尝试做这样的事情,问题是滚动事件没有按您希望的那样触发。 A nice workaround was subscribing the calculation function to the mousemove event, so it triggers A LOT. 一个不错的解决方法是将计算功能预订给mousemove事件,因此它会触发很多。 But on the other hand, I came up with another solution. 但另一方面,我想出了另一种解决方案。

Why not turn things around and ask yourself: Lets make it a position:fixed object and calculate what happens on resize. 为什么不把事情转过来问自己:让它成为一个position:fixed对象并计算调整大小时会发生什么。 Because you actually are trying to create a position-x:fixed; 因为您实际上是在尝试创建位置x:fixed; and a position-y:absolute; 位置y:绝对;

I actually did the following for the opposite kind of thing. 实际上,对于相反的事情,我做了以下工作。 A block that has to be exactly in the middle of the x-document, but in the y it was fixed. 必须完全位于x文档中间的块,但在y中,它是固定的。

$(document).ready(function ()
{
    replaceFixed();

    $(window).resize(replaceFixed);
    $('#content').ajaxSuccess(replaceFixed);
    $(window).scroll(replaceFixed);

    function replaceFixed()
    {
        var jEl = $('#centeredFixedContainer');

        var winW = $(window).width();
        var docW = $(document).width();
        var scrL = $(window).scrollLeft();
        var divW = jEl.width();

        var result = 0;

        // window bigger than the element
        if(winW > divW)
        {
            result = -scrL + ((docW-winW)/2);
        }
        else
        {
            result = $('#mainContainer').offset().left - scrL;
        }


        jEl.css('left',result);
    }
});

Copying this code will not give you the solution , but will indicate another way to look at your problem. 复制此代码不会给您解决方案 ,但会指出另一种解决问题的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM