简体   繁体   English

JavaScript滚动不会停止

[英]Javascript scroll doesn't stop

hi everyone i'm trying to do simple scroll share box widget but it doesn't work. 大家好,我正在尝试做简单的滚动共享框小部件,但它不起作用。 It must stop on special div (stopscroll), but it don't stop and scrolling down until web page footer. 它必须在特殊的div(停止滚动)上停止,但是直到网页页脚才停止并向下滚动。 any ideas why? 有什么想法吗?

var windw = this;

$.fn.followTo = function ( elem ) {
    var $this = this,
        $window = $(windw),
        $bumper = $(elem),
        bumperPos = $bumper.offset().top,
        thisHeight = $this.outerHeight(),
        setPosition = function(){
            if ($window.scrollTop() <= (bumperPos - thisHeight)) {
                $this.css({
                    position: 'absolute',
                    top: (bumperPos - thisHeight)
                });
            } else {
                $this.css({
                    position: 'fixed',
                    top: 0
                });
            }
        };
    $window.resize(function()
    {
        bumperPos = pos.offset().top;
        thisHeight = $this.outerHeight();
        setPosition();
    });
    $window.scroll(setPosition);
    setPosition();
};

$('#share_box').followTo('#stopscroll');

but it doesn't stop on the div #stopscroll. 但它不会在div #stopscroll上停止。

css file looks like that: CSS文件如下所示:

#share_box{
background: none repeat scroll 0% 0% #E1E1E1;
position: fixed;
width: 65px;
padding: 15px;
border-radius: 5px 0px 0px 5px;
left: 1.89%;}

any ideas? 有任何想法吗? here is jsfiddle http://jsfiddle.net/NCY6x/ 这是jsfiddle http://jsfiddle.net/NCY6x/

There are lots of syntax and variable mistakes in your code... 您的代码中有很多语法和变量错误。

I have updated the fiddle with a working and simpler demo: http://jsfiddle.net/NCY6x/2/ 我已经通过一个简单易用的演示更新了小提琴: http : //jsfiddle.net/NCY6x/2/

$.fn.followTo = function ( elem ) {
    var stopper = $(elem);
    var box = this;
    $(window).on("scroll resize", function(){
        var x_distance = (stopper.offset().top- box.outerHeight());
        if($(window).scrollTop() >= x_distance){
            box.css({"position": "absolute", "top": x_distance});
        } else {
            box.css({"position": "fixed", "top": 0});
        }
    });
};

$('#share_box').followTo('#stopscroll');

Well, in your Fiddle you haven't actually loaded jQuery , and then when loaded I get an error saying pos is not defined , which I think refers to the following line: 好吧,在您的小提琴中,您实际上没有加载jQuery ,然后在加载时出现错误,提示pos is not defined ,我认为这是指以下行:

bumperPos = pos.offset().top;

pos doesn't appear to be set anywhere pos似乎没有设置在任何地方

Edit: 编辑:

There were some other issues with your script. 您的脚本还有其他问题。 See here for a version that I think works as you intend http://jsfiddle.net/R5z6j/1/ 请参阅此处以获取我认为可以正常使用的版本http://jsfiddle.net/R5z6j/1/

I removed the padding on the top of #stopscroll as that doesn't move the element down so the top position was always being set to 18px (see the console output) 我删除了#stopscroll顶部的填充,因为该填充不会使元素向下移动,因此始终将顶部位置设置为18px(请参见控制台输出)

Edit 2: 编辑2:

http://jsfiddle.net/R5z6j/5/ - as you'll probably actually want it this way round http://jsfiddle.net/R5z6j/5/-您可能实际上希望这种方式

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

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