粘性侧栏在触摸设备上

时间:2014-04-29 22:09:52

标签: javascript jquery html css sticky

我有一个"粘"使用此插件的侧栏:https://github.com/AndrewHenderson/jSticky。基本上,它在桌面上运行良好,但在触摸设备上它会延迟一两秒,直到页面完全滚动以重新出现在适当的位置。是否有任何方法可以使页面在桌面上滚动时滚动?我已经看到其他网站上的粘性元素就像这样工作,所以应该有一种方法检查并实现位置更平滑。代码如下。您所做的就是为项目分配一个.sticky类。对于演示,您可以看到我正在处理的这个页面:http://www.mjmlawoffice.com/responsive/index.php/fees

<script>  
jQuery(function(){
jQuery(".sticky").sticky({
topSpacing: 0,
zIndex:2,
stopper: "#footer"
});
});
 </script>


;(function($) {

  $.fn.sticky = function(options) {

    var defaults = {
        topSpacing: 0, // No spacing by default
        zIndex: '', // No default z-index
        stopper: '.sticky-stopper' // Default stopper class, also accepts number value
      },
      settings = $.extend({}, defaults, options); // Accepts custom stopper id or class

    // Checks if custom z-index was defined
    function checkIndex() {

      if (typeof settings.zIndex == 'number') {
        return true;
      } else {
        return false;
      }
    }
    var hasIndex = checkIndex(); // True or false

    // Checks if a stopper exists in the DOM or number defined
    function checkStopper() {

      if ( 0 < $(settings.stopper).length || typeof settings.stopper === 'number' ) {
        return true;
      } else {
        return false;
      }
    }
    var hasStopper = checkStopper(); // True or false

    return this.each(function() {

      var $this          = $(this),
          thisHeight     = $this.outerHeight(),
          thisWidth      = $this.outerWidth(),
          topSpacing     = settings.topSpacing,
          zIndex         = settings.zIndex,
          pushPoint      = $this.offset().top - topSpacing, // Point at which the sticky element starts pushing
          placeholder    = $('<div></div>').width(thisWidth).height(thisHeight).addClass('sticky-placeholder'), // Cache a clone sticky element
          stopper        = settings.stopper,
          $window        = $(window);

      function stickyScroll() {

        var windowTop  = $window.scrollTop(); // Check window's scroll position

        if ( hasStopper && typeof stopper === 'string' ) {
          var stopperTop = $(stopper).offset().top,
              stopPoint  = (stopperTop - thisHeight) - topSpacing;
        } else if (hasStopper && typeof stopper === 'number') {
          var stopPoint = stopper;
        }

        if (pushPoint < windowTop) {
          // Create a placeholder for sticky element to occupy vertical real estate
          $this.after(placeholder).css({
            position: 'fixed',
            top: topSpacing
          });

          if (hasIndex) {
            $this.css({ zIndex: zIndex });
          }

          if (hasStopper) {
            if (stopPoint < windowTop) {
              var diff = (stopPoint - windowTop) + topSpacing;
              $this.css({ top: diff });
            }
          }
        } else {
          $this.css({
            position: 'static',
            top: null,
            left: null
          });

          placeholder.remove();
        }
      };

      $window.bind("scroll", stickyScroll);
    });
  };
})(jQuery);

1 个答案:

答案 0 :(得分:0)

这是因为@media screen and (min-width: 768px) *js-mainnav.megamenu-sticky的css选择器仅显示在最小宽度为768px的桌面上

不包括所需的属性不在较小的浏览器上。

尝试添加

#js-mainnav.megamenu-sticky {
    z-index: 1000;
   position: fixed !important;
}

在mediaquery之外