我是新来的jQuery,并希望做一个div向上滚动,并在顶部固定后,我向下滚动浏览器中的特定位置,就像http://www.airbnb.com/jobs/如何让一个div向上滚动,并在顶部
回答
写lik E本:
$(window).scroll(function() {
var height = $('body').height();
var scrollTop = $(window).scrollTop();
if(scrollTop>500){
$('#header').css({ 'position': 'fixed', 'top' : '0' });
}
else{
$('#header').css({ 'position': 'relative', 'top': '500px'});
}
});
修复请尝试以下步骤DEMO
这与我的其他答案(Answer)的几行值更改一致。原生JS解决方案。
的代码是:
JS:
window.onscroll=function() {
var top = window.pageXOffset ? window.pageXOffset : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
var head = document.getElementById("header")
if(top > 550){
head.style.position = "fixed";
head.style.height="50px"
head.style.top="0px"
}
else {
if(head.getAttribute("style"))
head.removeAttribute("style")
}
}
HTML:
<div class="container">
<div id="header" class="header">
Hello World
</div>
</div>
CSS:
.container{
height:2000px;
width:100%;
background-color:yellow;
}
.header{
text-align:center;
background-color:red;
height:100px;
position:relative;
min-height:50px;
width:100%;
top:500px;
}
希望它有助于
感谢您对我的建议 – user1496872 2012-07-24 09:31:45
+1,不过您还没有捕捉到的一个方面是每张图片的数量在您向下滚动时显示更改(这很漂亮)。 – Nick 2012-07-24 09:38:38
从我+1,不错的一段代码! – 2012-07-24 09:45:39
FIDDLE DEMO:http://jsfiddle.net/collabcoders/PZp8R/3/
扩展到包括jQuery的动画,你将需要包括jQuery的,jquery.effects.core.min.js,jquery.effects.slide.min .js和jquery.effects.transfer.min.js文件。
在你的CSS中,你会希望将div的上边距设置为负数,这将成为动画的起点。
CSS
.tophead {
width:100%;
height: 100px;
color:#fff;
background-color:#111;
}
.container {
height:2000px;
width:100%;
margin-top:-100px;
}
.header{
text-align:center;
background-color:#000;
height:100px;
position:relative;
min-height:100px;
width:100%;
top:500px;
color: #fff;
}
在你的JavaScript这将-100 PX动画全格下降到0在3秒内
的Javascript
$(document).ready(function() {
$(".container").animate({
marginTop: "0"
},2000);
});
$(window).scroll(function() {
var height = $('body').height();
var scrollTop = $(window).scrollTop();
if(scrollTop>500){
$('#header').css({ 'position': 'fixed', 'top' : '0' });
} else {
$('#header').css({ 'position': 'relative', 'top': '500px'});
}
});
HTML
<div class="container">
<div id="tophead" class="tophead">
Sliding Down Container
</div>
<div id="header" class="header">
Hello World
</div>
</div>
想要把它放在小提琴里吗? – Nick 2012-07-24 09:46:37
当然..我现在就开始工作 – johnnyarguelles 2012-07-24 09:53:56
http://jsfiddle.net/collabcoders/PZp8R/3/ – johnnyarguelles 2012-07-24 10:10:51
它很简单..只是像这样的例子:
$(window).scroll(function() {
var height = $('body').height();
var scrollTop = $(window).scrollTop();
if(scrollTop>100){
$('#header').css({ 'position': 'fixed', 'top' : '0', 'opacity': '0.7'});
} else {
$('#header').css({ 'position': 'relative', 'top': '100px', 'opacity': '1'});
}
});
- 1. Div在另一个div中滚动时顶部滚动
- 2. 动画div的顶部向下滚动
- 3. 如何让div向下滚动页面一旦到达页面顶部?
- 4. 滚动到下一个DIV从顶部
- 5. 如何一个div 15px的向上滑动到顶部
- 6. html - 如何让一个div元素停留在顶部,而你滚动?
- 7. .scrollTop(0)不工作让一个div滚动到顶部
- 8. 向下滚动时修复div顶部?
- 9. 如何滚动到底部或顶部的一个div溢出
- 10. 自动向下滚动div并返回顶部
- 11. 在滚动过另一个div后,让div粘到页面顶部?
- 12. 如何让UIWebView滚动到顶部?
- 13. 如何让div定位在另一个div的顶部角落?
- 14. 如何在滚动页面时让div停留在其他div的顶部?
- 15. 如何让div从顶部滚动时出现?
- 16. 如何让一个div停留在另一个div的底部滚动
- 17. 当mouseleave悬停以向下滚动div并自动滚动到顶部时
- 18. 滚动到顶部DIV
- 19. 滚动回div的顶部
- 20. 让div粘到另一个绝对定位的顶部滚动div
- 21. 如何从顶部向下滚动100px时使div消失?
- 22. 如何在div上创建两个滚动条作为顶部和底部?
- 23. 当在一个div向下滚动另一个格滚动向上
- 24. 粘div不粘在顶部在滚动
- 25. 如何让StackPanel在一个方向上滚动内容?
- 26. 如何让DIV留在顶部时rotatingX
- 27. 当滚动DIV隐藏的顶部的另一个DIV
- 28. 如何将滚动div内的项目滚动到顶部
- 29. 垂直移动(向上滚动)一个div在另一个
- 30. 滚动到顶部区域在DIV
哇,是的,这是很好的......好运气,止跌”没有线索从哪里开始! – freefaller 2012-07-24 09:15:34
这不完全一样,但几乎在同一线路上。 - http://stackoverflow.com/questions/9352419/fixed-header-transformation-when-page-is-scrolled/9352465#9352465 – 2012-07-24 09:17:38
有很多[ ** jQuery Parallax类型的滚动插件**](http://smashinghub.com/7-jquery-parallax-and-scrolling-effect-plugins.htm)具有强大的API来做很多事情。这里有一个[** SO Answer **](http://stackoverflow.com/a/10967523/1195891)我写了这个。 – arttronics 2012-07-24 10:17:52