Javascript JQUERY";单击以滚动“;单击任何内容时都会返回顶部
如果我点击“CV”,它将滚动到页面。如果我点击一个链接或照片,它会一直滚动到页面顶部Javascript JQUERY";单击以滚动“;单击任何内容时都会返回顶部,javascript,jquery,html,Javascript,Jquery,Html,如果我点击“CV”,它将滚动到页面。如果我点击一个链接或照片,它会一直滚动到页面顶部 // When the Document Object Model is ready jQuery(document).ready(function(){ // 'thisCVTopPosition' is the amount of pixels #thisCV // is from the top of the document var thisCVTopPosition =
// When the Document Object Model is ready
jQuery(document).ready(function(){
// 'thisCVTopPosition' is the amount of pixels #thisCV
// is from the top of the document
var thisCVTopPosition = jQuery('#thisCV').offset().top;
// When #scroll is clicked
jQuery('#cv').click(function(){
// Scroll down to 'thisCVTopPosition'
jQuery('html, body').animate({scrollTop:thisCVTopPosition}, 'fast');
// This stops the anchor link from acting like a normal anchor link
return false;
});
jQuery('#thisCV').parent().click(function(){
jQuery('html, body').animate({scrollTop:1}, 'fast');
return false;
})
});
我的导航栏:
<div id="sidebar-wrapper">
<center>
<ul class="sidebar-nav">
<li class="logo"><a href="#Home" class="scroll"><img src="img/logo2.png" alt=
"Portfollio" /></a></li>
<li style="list-style: none; display: inline">
<div class="animation">
<div class="hoverImages" align="center"><img src="img/photo.jpeg" alt=
"Photo" style="width:auto;height:auto;" /></div>
</div>
</li>
<li><a href="#Home" id = "home">Home</a></li>
<li><a href="#CV" id="cv">CV</a></li>
<li><a href="#Projects" id="projects">My Projects</a></li>
<li><a href="#Github" id="github">GitHub</a></li>
<li><a href="contact.php">Contact Me</a></li>
</ul>
</center>
</div>
-
简历
`
我的简历
[...]
`
是我的div还是jquery?
更新:修复了我的div,但它仍然不工作。锚元素有一个默认操作:转到它们的
href
url。当href
属性未设置(或我认为无效)时,默认操作是刷新页面(并转到顶部)
这可能就是发生在你身上的事情。您可以使用jQuery的event.preventDefault()
来防止任何事件触发其默认操作():
我在你的HTML中没有看到名为#thisCV的ID?你能用你的代码创建一个工作的JSFIDLE吗?我的猜测是,这与您使用click函数选择父元素有关,但我需要查看它的其余部分。
` <div id="page-content-wrapper">
<div class="container-fluid">
<div id="thisCV" style="height: 1000px; width 100px">
<h1>My CV</h1>
[...]
jQuery('#cv').click(function(event){
// Prevent the click from loading href
event.preventDefault();
// Scroll down to 'thisCVTopPosition'
jQuery('html, body').animate({scrollTop:thisCVTopPosition}, 'fast');
// This stops the anchor link from acting like a normal anchor link
return false;
});