修复了某些点jQuery的Div停止

时间:2016-04-03 07:13:08

标签: jquery html css

我希望我的固定div停止在我的网页上的某个点滚动,但是,我找到的所有解决方案都没有实际正常运行并完成了我想要的...

这是我目前的jQuery:

var windw = this;
$.fn.followTo = function ( pos ) {
    var $this = this, $window = $(windw);

    $window.scroll(function(e){
        if ($window.scrollTop() > pos) {
            $this.css({
                position: 'absolute',
                top: pos
            });
        } else {
            $this.css({
                position: 'fixed',
                bottom: -10px
            });
        }
    });
};

$('#qF').followTo(800);

这是我的HTML:

<div id="qF" class="central theater-dir-adown">
    <img src="data/img/prefs/dir_adown" class="dir-adown">
</div>

这是我的CSS:

.theater-dir-adown{
    cursor: pointer;
    display: inline-block;
    position: fixed;
    bottom: -10px;
    left: calc(100% / 2 - 51px);
    width: calc(75% / 9);
    height: auto;
}

.dir-adown{
    width: 100%;
}

所以我想要的是div #qF停止滚动800px,但我使用的代码不工作,div将继续向下滚动页面。我不确定我的代码中是否存在某种错误,但有人可以帮助我吗?对jQuery来说还是一个新手..

由于

1 个答案:

答案 0 :(得分:1)

在将位置设置为固定时添加顶部:'自动'

var windw = this;
$.fn.followTo = function ( pos ) {
    var $this = this, $window = $(windw);

    $window.scroll(function(e){
        if ($window.scrollTop() > pos) {
            $this.css({
                position: 'absolute',
                top: pos
            });
        } else {
            $this.css({
                position: 'fixed',
                bottom: -10,
                top:'auto'
            });
        }
    });
};

$('#qF').followTo(800);

你可以添加top:pos + $(window).height()来开始从底部滚动

http://jsbin.com/fuzutaxiha/1/edit?html,css,js,output

相关问题