简体   繁体   English

到达视口中间时如何固定div位置?

[英]How to fix div position when it reaches middle of viewport?

I have reference image attach for description.As in figure it is having two section side by side.One is for image(mobile and tablet) and second is for is features.This whole section is below the fold. 我有参考图像附件进行描述。如图所示,它有两个并排的部分。一个是图像(手机和平板电脑),第二个是功能部件。整个部分都在折叠以下。

Now when user scrolls to view it and when image reaches vertically middle of the viewport its position should remain fixed.While its position is fixed features should scroll as normally.As one feature goes out of viewport image should change. 现在,当用户滚动以查看它时,当图像垂直到达视口的中间时,其位置应保持固定。虽然其位置是固定的,但要素应正常滚动。当一个要素退出视口时,图像应更改。 When all features are out of viewport, position of image should again become static. 当所有要素均不在视口中时,图像位置应再次变为静态。

I am using skrollr js plugin for this.Please help... 我正在为此使用skrollr js插件。请帮助...

Here is the code 这是代码

 .featurenav { margin: 0px; padding: 0px; list-style: none; } .featurenav li:before { content: url('../images/point.png'); position: absolute; left: 0px; top: 5px } .featurenav li { font-family: OpenSans-Light; color: #838b80; font-size: 30px; margin-bottom: 90px; padding-left: 83px; position: relative; } .color { float: left; width: 74px; height: 74px; margin: 30px 25px 0px 0px; border-radius: 5px; } .color1 { background-color: #c9bda3; } .color2 { background-color: #c99a32; } .color3 { background-color: #838b80; } .color4 { background-color: #fff; } .showcase img { width: 100%; } .showcase img:not(:first-child) { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-xs-12 padding-zero"> <div class="col-sm-6"> <ul class="featurenav"> <li class="firstli">Graphical Layout</li> <li class="secondli">Open sans Font Used</li> <li class="thirdli">Colors Used <br> <div class="color color1"></div> <div class="color color2"></div> <div class="color color3"></div> <div class="color color4"></div> <div class="clearfix"></div> </li> <li class="fourthli">Parallax Smooth Scrolling</li> <li class="fifthli">Adaptable</li> </ul> </div> <div class="col-sm-6 showcase"> <img src="images/mobile_tablet.png" data-bottom-top="display:block" alt="showcase" data-anchor-target=".firstli" /> <img src="images/mobile_tablet_2.png" alt="showcase" /> <img src="images/mobile_tablet_3.png" alt="showcase" /> <img src="images/mobile_tablet_4.png" alt="showcase" /> <img src="images/mobile_tablet_5.png" alt="showcase" /> </div> <div class="clearfix"></div> </div> 

在此处输入图片说明

Your images aren't working, so here's a generic solution. 您的图片无效,因此这是一个通用解决方案。 All you have to do is change the element to position: fixed; 您所要做的就是将元素更改为position: fixed; when you scroll to wherever the image is minus half the browser viewport height 当您滚动到图像减去浏览器视口高度一半的位置时

 $(window).on('load', function() { $affix = $('#affix'); $affix.attr('data-affix', $affix.offset().top); }).on('scroll', function() { var scrolled = $(window).scrollTop(), vh = $(window).height(), halfView = (vh / 2), scrollPoint = $affix.attr('data-affix') - halfView; if (scrolled >= scrollPoint) { $('img').addClass('fixed'); } else { $('img').removeClass('fixed'); } }); 
 * {margin:0;} section { height: 500vh; } img { max-width: 600px; width: 100%; } .fixed { position: fixed; top: 50%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <section></section> <section><img id="affix" src="http://plusquotes.com/images/quotes-img/cool_cat.jpg" data-affix></section> 

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

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