简体   繁体   English

滚动时将div粘贴到屏幕顶部

[英]Sticky div to top of screen when scrolled

I have a Wordpress site with a Jumbotron, inside is a div 'sticky' which is positioned:absolute to the bottom. 我有一个Jumbotron的Wordpress网站,里面是一个div'sticky',它的位置是:绝对位于底部。 The jumbotron has a fixed height and I would like the div to 'stick' to the top of the screen when scrolled to. 巨无霸具有固定的高度,我希望div滚动到时可以“粘”在屏幕顶部。

I have seen multiple threads/examples on the subject but none seem to work for me (most not written in a format that works with/for Wordpress) 我在该主题上看到了多个线程/示例,但似乎没有一个适合我(大多数不是以适用于Wordpress的格式编写的)

What is the best approach to making it work in WP (using function.php, etc) 使它在WP中工作的最佳方法是什么(使用function.php等)

HTML 的HTML

<div class="row col-md-12">
 <div class="jumbotron">
    <div class="sticky"><p>CURRENT WORK</p></div>
 </div>
</div><!-- /row -->

CSS 的CSS

.jumbotron {
  margin: 0;
  width: 100%;
  height: 400px;
  background-color: #fff;
}
.sticky {
  position: absolute;
  bottom: 0; right: 0;
  background-color: red;
  padding: 0 5px;
  width: 200px;
  text-align: center;
}

First of all, give .jumbotron a relative positioning 首先,给.jumbotron一个相对的位置

position: relative;

And .sticky a height value .sticky一个高度值

height:40px;

You will also need to add an anchor DIV inside the sticky DIV 您还需要在粘性DIV中添加锚点DIV

<div class="sticky"><div id="anchor"></div><p>CURRENT WORK</p></div>

and then the following javascript 然后下面的javascript

$(document).ready(function(){       
   var scroll_start = 0;
   var startchange = $('#anchor');
   var offset = startchange.offset();
    if (startchange.length){
   $(document).scroll(function() { 
      scroll_start = $(this).scrollTop();
      if(scroll_start > offset.top) {
          $('.sticky').css('position', 'fixed');
          $('.sticky').css('top', '0');
          $('.sticky').css('z-index', '10000');
       } 
   });
    }
});

See it here: http://jsfiddle.net/ubLgaktp/ 在这里查看: http : //jsfiddle.net/ubLgaktp/

EDIT: you can add the javascript to the page itself by putting the following between your head tags 编辑:您可以通过在您的head标签之间放置以下内容来将javascript添加到页面本身

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"</script>
<script>
$(document).ready(function(){       
   var scroll_start = 0;
   var startchange = $('#anchor');
   var offset = startchange.offset();
    if (startchange.length){
   $(document).scroll(function() { 
      scroll_start = $(this).scrollTop();
      if(scroll_start > offset.top) {
          $('.sticky').css('position', 'fixed');
          $('.sticky').css('top', '0');
          $('.sticky').css('z-index', '10000');
       } 
   });
    }
});
</script>

EDIT: 编辑:

Here is a better approach. 这是一个更好的方法。 Use the anchor div to add and remove a class to the div as opposed to changing the class. 使用锚点div将类添加和删除到div中,而不是更改类。 This way the sticky div become 'unstuck' if the viewer scrolls back down. 这样,如果观看者向下滚动,粘性div就会变成“未粘住”状态。

Here is the jsfiddle... 这是jsfiddle ...

http://jsfiddle.net/bh40k8Lg/ http://jsfiddle.net/bh40k8Lg/

the HTML HTML

<div class="row col-md-12">
 <div class="jumbotron">
    <div id="sticky"><div id="anchor"></div><p>CURRENT WORK</p></div>     
 </div>
</div><!-- /row -->

the CSS CSS

.jumbotron {
  margin: 0;
  width: 100%;
  height: 400px;
  background-color: #fff;
  position: relative;
}

#sticky {
  position: absolute;
  bottom: 0; right: 0;
  background-color: red;
  padding: 0 5px;
  width: 200px;
  text-align: center;
    height:40px;
}

.sticky-top {
    position:fixed!important;
    top:0!important;
    z-index:10000!important;
}

the Javascript which you can put between your head tags 您可以在首标之间插入的Javascript

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
 <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.11.2.min.js"><\/script>')</script>
<script>
$(document).ready(function(){       
   var scroll_start = 0;
   var startchange = $('#anchor');
   var offset = startchange.offset();
    if (startchange.length){
   $(document).scroll(function() { 
      scroll_start = $(this).scrollTop();
      if(scroll_start > offset.top) {
        document.getElementById('sticky').classList.add('sticky-top');
       } else {
        document.getElementById('sticky').classList.remove('sticky-top');
       }
   });
    }
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM