0

我在我的网站上使用粘性。它在 CSS 上完美配置,但是当我开始滚动时,它会转到左侧。我希望它粘在它所在的右侧。我怎样才能做到这一点?

我确定它是 JS 文件的一部分

(function($){
$.fn.extend({ 
    //plugin name - jsticky
    jsticky: function(options) {

        //Settings list and the default values
        var defaults = {
            marginTop: 0
        };

        var options = $.extend(defaults, options);

        return this.each(function() {
            var o = options;

            //Assign current element to variable, in this case is UL element
            var obj = $(this);   


var offset = obj.offset();
var topOffset = offset.top;
var marginTop = obj.css("marginTop");
var marginTopNum = parseInt(marginTop);

var diff = topOffset - marginTopNum;


var offset = obj.offset();
            var topOffset = offset.top;
            var leftOffset = offset.left;
            var marginTop = obj.css("marginTop");
            var marginLeft = obj.css("marginLeft");


            $(window).scroll(function() { 
                var WindowScrollTop = $(window).scrollTop();
                var scrollTop = WindowScrollTop + o.marginTop;

                if (scrollTop >= topOffset){

                    obj.css({
                        marginTop: o.marginTop,
                        marginLeft: leftOffset,
                        position: 'fixed'
                    });
                }

                if (scrollTop < topOffset){

                    obj.css({
                        marginTop: marginTop,
                        marginLeft: marginLeft,
                        position: 'fixed'
                    });
                }
            });


        });
    }
});
})(jQuery);
4

1 回答 1

0

试试这个:

if (scrollTop >= topOffset){
    obj.css({
        marginTop: o.marginTop,
        marginLeft: marginLeft, //<----change to this
        position: 'fixed'
    });
}
于 2013-02-21T09:59:54.970 回答