【问题标题】:How to fix div on scroll [closed]如何在滚动上修复 div [关闭]
【发布时间】:2020-01-05 09:04:48
【问题描述】:

如果您在以下 URL 中向下滚动页面,“共享”div 将锁定到浏览器:

http://knowyourmeme.com/memes/pizza-is-a-vegetable

我假设他们正在应用 position: fixed; 属性。 jQuery如何实现这一点?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    您可以在下面找到一个示例。基本上,您将一个函数附加到windowscroll 事件并跟踪scrollTop 属性,如果它高于所需的阈值,则应用position: fixed 和其他一些css 属性。

    jQuery(function($) {
      $(window).scroll(function fix_element() {
        $('#target').css(
          $(window).scrollTop() > 100
            ? { 'position': 'fixed', 'top': '10px' }
            : { 'position': 'relative', 'top': 'auto' }
        );
        return fix_element;
      }());
    });
    body {
      height: 2000px;
      padding-top: 100px;
    }
    code {
      padding: 5px;
      background: #efefef;
    }
    #target {
      color: #c00;
      font: 15px arial;
      padding: 10px;
      margin: 10px;
      border: 1px solid #c00;
      width: 200px;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="target">This <code>div</code> is going to be fixed</div>

    【讨论】:

    • @Emre - 如果我使用 $(window).scroll(fixDiv).resize(fixDiv); 而不是 $(window).scroll(fixDiv); fixDiv(); 会怎样
    【解决方案2】:

    关于设计师的 jQuery 有一篇写得很好的文章,这就是 jQuery sn-p 的神奇之处。只需将 #comment 替换为要浮动的 div 的选择器即可。

    注意:要查看整篇文章,请转到此处:http://jqueryfordesigners.com/fixed-floating-elements/

    $(document).ready(function () {
      var $obj = $('#comment');
      var top = $obj.offset().top - parseFloat($obj.css('marginTop').replace(/auto/, 0));
    
      $(window).scroll(function (event) {
        // what the y position of the scroll is
        var y = $(this).scrollTop();
    
        // whether that's below the form
        if (y >= top) {
          // if so, ad the fixed class
          $obj.addClass('fixed');
        } else {
          // otherwise remove it
          $obj.removeClass('fixed');
        }
      });
    });
    

    【讨论】:

    • 当您将 var 名称“top”更改为另一个名称时效果很好,但是替换所有#comment 有点讨厌,您应该使用一个对象。
    【解决方案3】:

    我在这里混合了答案,取了@Julian 的代码和其他人的想法,对我来说似乎更清楚,这就是剩下的:

    fiddlehttp://jsfiddle.net/wq2Ej/

    jquery

    //store the element
    var $cache = $('.my-sticky-element');
    
    //store the initial position of the element
    var vTop = $cache.offset().top - parseFloat($cache.css('marginTop').replace(/auto/, 0));
      $(window).scroll(function (event) {
        // what the y position of the scroll is
        var y = $(this).scrollTop();
    
        // whether that's below the form
        if (y >= vTop) {
          // if so, ad the fixed class
          $cache.addClass('stuck');
        } else {
          // otherwise remove it
          $cache.removeClass('stuck');
        }
      });
    

    css:

    .my-sticky-element.stuck {
        position:fixed;
        top:0;
        box-shadow:0 2px 4px rgba(0, 0, 0, .3);
    }
    

    【讨论】:

      【解决方案4】:
      (function($) {
        var triggers = [];
        $.fn.floatingFixed = function(options) {
          options = $.extend({}, $.floatingFixed.defaults, options);
          var r = $(this).each(function() {
            var $this = $(this), pos = $this.position();
            pos.position = $this.css("position");
            $this.data("floatingFixedOrig", pos);
            $this.data("floatingFixedOptions", options);
            triggers.push($this);
          });
          windowScroll();
          return r;
        };
      
        $.floatingFixed = $.fn.floatingFixed;
        $.floatingFixed.defaults = {
          padding: 0
        };
      
        var $window = $(window);
        var windowScroll = function() {
          if(triggers.length === 0) { return; }
          var scrollY = $window.scrollTop();
          for(var i = 0; i < triggers.length; i++) {
            var t = triggers[i], opt = t.data("floatingFixedOptions");
            if(!t.data("isFloating")) {
              var off = t.offset();
              t.data("floatingFixedTop", off.top);
              t.data("floatingFixedLeft", off.left);
            }
            var top = top = t.data("floatingFixedTop");
            if(top < scrollY + opt.padding && !t.data("isFloating")) {
              t.css({position: 'fixed', top: opt.padding, left: t.data("floatingFixedLeft"), width: t.width() }).data("isFloating", true);
            } else if(top >= scrollY + opt.padding && t.data("isFloating")) {
              var pos = t.data("floatingFixedOrig");
              t.css(pos).data("isFloating", false);
            }
          }
        };
      
        $window.scroll(windowScroll).resize(windowScroll);
      })(jQuery);
      

      然后通过调用将任何 div 设为浮动固定

      $('#id of the div').floatingFixed();
      

      来源:https://github.com/cheald/floatingFixed

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-12-15
        • 2014-12-02
        • 1970-01-01
        • 2013-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多