我的网站上有一个侧面菜单,我希望它始终可见. 为此,我使用此页面中的以下代码示例: http://camwebdesign.com/demos/jquery-scrolling-element.html 问题是,与示例不同,我的网站有一个巨大的1000px高度
为此,我使用此页面中的以下代码示例:
http://camwebdesign.com/demos/jquery-scrolling-element.html
问题是,与示例不同,我的网站有一个巨大的1000px高度页脚.
当用户在页脚div上滚动内容时,侧边菜单会重叠它.
有没有办法修改Jquery代码,它底部有一个1000px的边界,以防止这种情况?
谢谢!
<html> <title>Keep element in view while scrolling using JQuery</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js?ver=CDN"></script> <script> $().ready(function() { var $scrollingDiv = $("#scrollingDiv"); $(window).scroll(function(){ $scrollingDiv .stop() .animate({"marginTop": ($(window).scrollTop() + 30) + "px"}, "slow" ); }); }); </script> <div style="float: left; width: 70%"> <h1>Keep element in view while scrolling using JQuery</h1>
<div id="scrollingDiv" style="position: absolute; top: 32px; right: 16px; width: 21%; padding: 0% 2% 2% 2%; border: 2px solid red; background-color: #ffeaea;"> <h2>Scrolling Element</h2> Scroll down/up to see me smoothly reposition myself and keep in view.<br /><br /> <em>Smooooooooooooooooooth</em><br /> </div> <div style="clear:both;"></div> <div id="footer" style="width:100%; height:1000px; background:#333; color:#FFF;" > <h1>Footer</h1> </div>演示
http://jsfiddle.net/NsfwM/
全屏
http://jsfiddle.net/NsfwM/embedded/result/
JS
var $scrollingDiv = $("#scrollingDiv"); $(window).scroll(function(){ var y = $(this).scrollTop(), maxY = $('#footer').offset().top, scrollHeight = $scrollingDiv.height(); if(y< maxY-scrollHeight ){ $scrollingDiv .stop() .animate({"marginTop": ($(window).scrollTop()) + "px"}, "slow" ); } });
还有另一个你的30px偏移量
http://jsfiddle.net/NsfwM/1/