粘性滚动小部件

时间:2018-06-01 05:13:59

标签: javascript jquery html css

我正在尝试添加一个粘性元素,该元素将与用户在网页上的进度同步滚动。

我尝试将简单的粘性元素添加到上一个图片库设计中,然后在Codepen和我自己的服务器上进行测试。

Codepen目前在jQuery资源上引发错误;但是,元素的功能在我的服务器上“工作”(只是div几乎完全被窗口的右侧剪切)。我认为css / css.styles中的选项display: flexflex-wrap可能会导致问题。

我想澄清,希望是一个相对简单的解决方案。

// Sticky Plugin v1.0.4 for jQuery
// =============
// Author: Anthony Garand
// Improvements by German M. Bravo (Kronuz) and Ruud Kamphuis (ruudk)
// Improvements by Leonardo C. Daronco (daronco)
// Created: 02/14/2011
// Date: 07/20/2015
// Website: http://stickyjs.com/
// Description: Makes an element on the page stick on the screen as you scroll
//              It will only set the 'top' and 'position' of your element, you
//              might need to adjust the width in some cases.

(function (factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(['jquery'], factory);
    } else if (typeof module === 'object' && module.exports) {
        // Node/CommonJS
        module.exports = factory(require('jquery'));
    } else {
        // Browser globals
        factory(jQuery);
    }
}(function ($) {
    var slice = Array.prototype.slice; // save ref to original slice()
    var splice = Array.prototype.splice; // save ref to original slice()

  var defaults = {
      topSpacing: 0,
      bottomSpacing: 0,
      className: 'is-sticky',
      wrapperClassName: 'sticky-wrapper',
      center: false,
      getWidthFrom: '',
      widthFromWrapper: true, // works only when .getWidthFrom is empty
      responsiveWidth: false,
      zIndex: 'inherit'
    },
    $window = $(window),
    $document = $(document),
    sticked = [],
    windowHeight = $window.height(),
    scroller = function() {
      var scrollTop = $window.scrollTop(),
        documentHeight = $document.height(),
        dwh = documentHeight - windowHeight,
        extra = (scrollTop > dwh) ? dwh - scrollTop : 0;

      for (var i = 0, l = sticked.length; i < l; i++) {
        var s = sticked[i],
          elementTop = s.stickyWrapper.offset().top,
          etse = elementTop - s.topSpacing - extra;

        //update height in case of dynamic content
        s.stickyWrapper.css('height', s.stickyElement.outerHeight());

        if (scrollTop <= etse) {
          if (s.currentTop !== null) {
            s.stickyElement
              .css({
                'width': '',
                'position': '',
                'top': '',
                'z-index': ''
              });
            s.stickyElement.parent().removeClass(s.className);
            s.stickyElement.trigger('sticky-end', [s]);
            s.currentTop = null;
          }
        }
        else {
          var newTop = documentHeight - s.stickyElement.outerHeight()
            - s.topSpacing - s.bottomSpacing - scrollTop - extra;
          if (newTop < 0) {
            newTop = newTop + s.topSpacing;
          } else {
            newTop = s.topSpacing;
          }
          if (s.currentTop !== newTop) {
            var newWidth;
            if (s.getWidthFrom) {
                padding =  s.stickyElement.innerWidth() - s.stickyElement.width();
                newWidth = $(s.getWidthFrom).width() - padding || null;
            } else if (s.widthFromWrapper) {
                newWidth = s.stickyWrapper.width();
            }
            if (newWidth == null) {
                newWidth = s.stickyElement.width();
            }
            s.stickyElement
              .css('width', newWidth)
              .css('position', 'fixed')
              .css('top', newTop)
              .css('z-index', s.zIndex);

            s.stickyElement.parent().addClass(s.className);

            if (s.currentTop === null) {
              s.stickyElement.trigger('sticky-start', [s]);
            } else {
              // sticky is started but it have to be repositioned
              s.stickyElement.trigger('sticky-update', [s]);
            }

            if (s.currentTop === s.topSpacing && s.currentTop > newTop || s.currentTop === null && newTop < s.topSpacing) {
              // just reached bottom || just started to stick but bottom is already reached
              s.stickyElement.trigger('sticky-bottom-reached', [s]);
            } else if(s.currentTop !== null && newTop === s.topSpacing && s.currentTop < newTop) {
              // sticky is started && sticked at topSpacing && overflowing from top just finished
              s.stickyElement.trigger('sticky-bottom-unreached', [s]);
            }

            s.currentTop = newTop;
          }

          // Check if sticky has reached end of container and stop sticking
          var stickyWrapperContainer = s.stickyWrapper.parent();
          var unstick = (s.stickyElement.offset().top + s.stickyElement.outerHeight() >= stickyWrapperContainer.offset().top + stickyWrapperContainer.outerHeight()) && (s.stickyElement.offset().top <= s.topSpacing);

          if( unstick ) {
            s.stickyElement
              .css('position', 'absolute')
              .css('top', '')
              .css('bottom', 0)
              .css('z-index', '');
          } else {
            s.stickyElement
              .css('position', 'fixed')
              .css('top', newTop)
              .css('bottom', '')
              .css('z-index', s.zIndex);
          }
        }
      }
    },
    resizer = function() {
      windowHeight = $window.height();

      for (var i = 0, l = sticked.length; i < l; i++) {
        var s = sticked[i];
        var newWidth = null;
        if (s.getWidthFrom) {
            if (s.responsiveWidth) {
                newWidth = $(s.getWidthFrom).width();
            }
        } else if(s.widthFromWrapper) {
            newWidth = s.stickyWrapper.width();
        }
        if (newWidth != null) {
            s.stickyElement.css('width', newWidth);
        }
      }
    },
    methods = {
      init: function(options) {
        return this.each(function() {
          var o = $.extend({}, defaults, options);
          var stickyElement = $(this);

          var stickyId = stickyElement.attr('id');
          var wrapperId = stickyId ? stickyId + '-' + defaults.wrapperClassName : defaults.wrapperClassName;
          var wrapper = $('<div></div>')
            .attr('id', wrapperId)
            .addClass(o.wrapperClassName);

          stickyElement.wrapAll(function() {
            if ($(this).parent("#" + wrapperId).length == 0) {
                    return wrapper;
            }
});

          var stickyWrapper = stickyElement.parent();

          if (o.center) {
            stickyWrapper.css({width:stickyElement.outerWidth(),marginLeft:"auto",marginRight:"auto"});
          }

          if (stickyElement.css("float") === "right") {
            stickyElement.css({"float":"none"}).parent().css({"float":"right"});
          }

          o.stickyElement = stickyElement;
          o.stickyWrapper = stickyWrapper;
          o.currentTop    = null;

          sticked.push(o);

          methods.setWrapperHeight(this);
          methods.setupChangeListeners(this);
        });
      },

      setWrapperHeight: function(stickyElement) {
        var element = $(stickyElement);
        var stickyWrapper = element.parent();
        if (stickyWrapper) {
          stickyWrapper.css('height', element.outerHeight());
        }
      },

      setupChangeListeners: function(stickyElement) {
        if (window.MutationObserver) {
          var mutationObserver = new window.MutationObserver(function(mutations) {
            if (mutations[0].addedNodes.length || mutations[0].removedNodes.length) {
              methods.setWrapperHeight(stickyElement);
            }
          });
          mutationObserver.observe(stickyElement, {subtree: true, childList: true});
        } else {
          if (window.addEventListener) {
            stickyElement.addEventListener('DOMNodeInserted', function() {
              methods.setWrapperHeight(stickyElement);
            }, false);
            stickyElement.addEventListener('DOMNodeRemoved', function() {
              methods.setWrapperHeight(stickyElement);
            }, false);
          } else if (window.attachEvent) {
            stickyElement.attachEvent('onDOMNodeInserted', function() {
              methods.setWrapperHeight(stickyElement);
            });
            stickyElement.attachEvent('onDOMNodeRemoved', function() {
              methods.setWrapperHeight(stickyElement);
            });
          }
        }
      },
      update: scroller,
      unstick: function(options) {
        return this.each(function() {
          var that = this;
          var unstickyElement = $(that);

          var removeIdx = -1;
          var i = sticked.length;
          while (i-- > 0) {
            if (sticked[i].stickyElement.get(0) === that) {
                splice.call(sticked,i,1);
                removeIdx = i;
            }
          }
          if(removeIdx !== -1) {
            unstickyElement.unwrap();
            unstickyElement
              .css({
                'width': '',
                'position': '',
                'top': '',
                'float': '',
                'z-index': ''
              })
            ;
          }
        });
      }
    };

  // should be more efficient than using $window.scroll(scroller) and $window.resize(resizer):
  if (window.addEventListener) {
    window.addEventListener('scroll', scroller, false);
    window.addEventListener('resize', resizer, false);
  } else if (window.attachEvent) {
    window.attachEvent('onscroll', scroller);
    window.attachEvent('onresize', resizer);
  }

  $.fn.sticky = function(method) {
    if (methods[method]) {
      return methods[method].apply(this, slice.call(arguments, 1));
    } else if (typeof method === 'object' || !method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error('Method ' + method + ' does not exist on jQuery.sticky');
    }
  };

  $.fn.unstick = function(method) {
    if (methods[method]) {
      return methods[method].apply(this, slice.call(arguments, 1));
    } else if (typeof method === 'object' || !method ) {
      return methods.unstick.apply( this, arguments );
    } else {
      $.error('Method ' + method + ' does not exist on jQuery.sticky');
    }
  };
  $(function() {
    setTimeout(scroller, 0);
  });
}));
#sticker {
      background: #bada55;
      color: white;
      width: 300px;
      font-family: Droid Sans;
      font-size: 40px;
      line-height: 1.6em;
      font-weight: bold;
      text-align: center;
      padding: 20px;
      text-shadow: 0 1px 1px rgba(0,0,0,.2);
      border-radius: 50px;
      float:right;
      margin-right: 200px;
    }

.div-table {
  display: flex;
  flex-wrap: wrap;
  width: 400px;
}

.div-table img {
  float: left;
  width: 100%;
}

.div-table span {
  background: #cff;
  float: left;
  height: 100px;
  left: 100%;
  overflow: hidden;
  position: absolute;
  transition: width 2s;
  transition-delay: 1s;
  width: 0%;
}

.div-table div {
  cursor: pointer;
  flex: 0 0 100px;
  height: 100px;
  position: relative;
  transition: 1s transform;
}

.div-table div:hover {
  transform: scale(2, 2);
  z-index: 2;
}

.div-table div:hover span {
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="sticker">
      <p>This is the sticky thingy that is really cool.</p>
</div>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<div class="div-table">
  <div>
    <img src="https://placeimg.com/400/400/nature" />
    <span>Crouching Tiger, Hidden Text</span>
  </div>
  <div>
    <img src="https://placeimg.com/400/400/people" />
    <span>Crouching Tiger, Hidden Text</span>
  </div>
  <div>
    <img src="https://placeimg.com/400/400/architecture" />
    <span>Crouching Tiger, Hidden Text</span>
  </div>
  <div>
    <img src="https://placeimg.com/400/400/animals" />
    <span>Crouching Tiger, Hidden Text</span>
  </div>
  <div>
    <img src="https://placeimg.com/400/400/animals" />
    <span>Crouching Tiger, Hidden Text</span>
  </div>
  <div>
    <img src="https://placeimg.com/400/400/sepia" />
    <span>Crouching Tiger, Hidden Text</span>
  </div>
  <div>
    <img src="https://placeimg.com/400/400/grayscale" />
    <span>Crouching Tiger, Hidden Text</span>
  </div>
  <div>
    <img src="https://placeimg.com/400/400/tech" />
    <span>Crouching Tiger, Hidden Text</span>
  </div>
</div>

<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>
<h2>text</h2>

http://paws.freedynamicdns.org/test/

1 个答案:

答案 0 :(得分:0)

查看由#stickerfloat: right;构成的margin-right: 200px; CSS 样式,了解#sticker元素推出的原因当用户滚动时。简单的分辨率,如果您可以使用 Bootstrap ,这将允许您分开两列而不是一列内的所有内容。看看我是怎么做到的:

$(document).ready(function() {
  $("#sticker").sticky({
    topSpacing: 0
  });
});
#sticker {
  background: #bada55;
  color: white;
  width: 100%;
  max-width: 300px;
  font-family: Droid Sans;
  font-size: 40px;
  line-height: 1.6em;
  font-weight: bold;
  text-align: center;
  padding: 10px;
  text-shadow: 0 1px 1px rgba(0, 0, 0, .2);
  border-radius: 50px;
}

.div-table {
  display: flex;
  flex-wrap: wrap;
  width: 400px;
}

.div-table img {
  float: left;
  width: 100%;
}

.div-table span {
  background: #cff;
  float: left;
  height: 100px;
  left: 100%;
  overflow: hidden;
  position: absolute;
  transition: width 2s;
  transition-delay: 1s;
  width: 0%;
}

.div-table div {
  cursor: pointer;
  flex: 0 0 100px;
  height: 100px;
  position: relative;
  transition: 1s transform;
}

.div-table div:hover {
  transform: scale(2, 2);
  z-index: 2;
}

.div-table div:hover span {
  width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.sticky/1.0.4/jquery.sticky.js"></script>
<div class="container">
  <div class="row">
    <div class="col-sm-6">
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <div class="div-table">
        <div>
          <img src="https://placeimg.com/400/400/nature" />
          <span>Crouching Tiger, Hidden Text</span>
        </div>
        <div>
          <img src="https://placeimg.com/400/400/people" />
          <span>Crouching Tiger, Hidden Text</span>
        </div>
        <div>
          <img src="https://placeimg.com/400/400/architecture" />
          <span>Crouching Tiger, Hidden Text</span>
        </div>
        <div>
          <img src="https://placeimg.com/400/400/animals" />
          <span>Crouching Tiger, Hidden Text</span>
        </div>
        <div>
          <img src="https://placeimg.com/400/400/animals" />
          <span>Crouching Tiger, Hidden Text</span>
        </div>
        <div>
          <img src="https://placeimg.com/400/400/sepia" />
          <span>Crouching Tiger, Hidden Text</span>
        </div>
        <div>
          <img src="https://placeimg.com/400/400/grayscale" />
          <span>Crouching Tiger, Hidden Text</span>
        </div>
        <div>
          <img src="https://placeimg.com/400/400/tech" />
          <span>Crouching Tiger, Hidden Text</span>
        </div>
      </div>

      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
      <h2>text</h2>
    </div>
    <div class="col-sm-6">
      <div id="sticker">
        <p>This is the sticky thingy that is really cool.</p>
      </div>
    </div>
  </div>
</div>