禁用位置:固定在距页面底部250px时滚动

时间:2019-04-09 07:22:06

标签: javascript jquery html css

$(window).scroll(function() {
  $(".lay-fixed").css("bottom", Math.max(0, 250 - $(this).scrollBottom()));
});
.lay-fixed {
  position: fixed;
  right: 30px
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-md-6 lay-fixed" id="gotobooking">
  ----widget code-----
</div>

在这里,我尝试从页面底部250px滚动时禁用position:fixed元素。帮助我找出解决此问题的方法。

1 个答案:

答案 0 :(得分:1)

请尝试:

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

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

$('.lay-fixed').followTo(250);
.lay-fixed {
  position: fixed;
  right: 30px
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-md-6 lay-fixed" id="gotobooking">
     ----widget code-----

</div>
<div>
  But I must explain to you how all<br /> this mistaken idea of denouncing pleasure and praising<br /> pain was born and I will give you a complete account of the<br /> system, and expound the actual teachings of the great<br /> explorer of the truth, the master-builder of human happiness.<br /> No one rejects, dislikes, or avoids pleasure itself, because<br /> it is pleasure, but because those who do not know how<br /> to pursue pleasure rationally encounter consequences<br /> that are extremely painful. Nor again is there anyone<br /> who loves or pursues or desires to obtain pain of itself,<br /> because it is pain, but because occasionally circumstances<br /> occur in which toil and pain can procure him some great pleasure.<br /> To take a trivial example, which of us ever undertakes<br /> laborious physical exercise, except to obtain some<br /> advantage from it? But who has any right to find fault with<br /> a man who chooses to enjoy a pleasure that has no annoying<br /> consequ
  But I must explain to you how<br /> all this mistaken idea of denouncing pleasure and praising pain<br /> was born and I will give you a complete account of the system,<br /> and expound the actual teachings of the great explorer of<br /> the truth, the master-builder of human happiness. No one rejects,<br /> dislikes, or avoids pleasure itself, because it is pleasure,<br /> but because those who do not know how to pursue<br /> pleasure rationally encounter consequences that are<br /> extremely painful. Nor again is there anyone who loves<br /> or pursues or desires to obtain pain of itself, because it is pain,<br /> but because occasionally circumstances<br /> occur in which toil and pain can procure him some great pleasure.<br /> To take a trivial example, which of us ever undertakes<br /> laborious physical exercise, except to obtain some advantage<br /> from it? But who has any right to find fault with a man who<br /> chooses to enjoy a pleasure that has no annoying consequ<br />
  But I must explain to you how all<br /> this mistaken idea of denouncing pleasure and praising pain<br /> was born and I will give you a complete account of the system,<br /> and expound the actual teachings of the great explorer of<br /> the truth, the master-builder of human happiness. No one rejects,<br /> dislikes, or avoids pleasure itself, because it is pleasure,<br /> but because those who do not know how to pursue<br /> pleasure rationally encounter consequences that are<br /> extremely painful. Nor again is there anyone who loves<br /> or pursues or desires to obtain pain of itself, because<br /> it is pain, but because occasionally circumstances occur<br /> in which toil and pain can procure him some great pleasure.<br /> To take a trivial example, which of us ever undertakes laborious<br /> physical exercise, except to obtain some advantage from it?<br /> But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequ
</div>