单击菜单 link 从一个 div 移动到另一个 link
Moving from one div to another on click of a menu link
我想在我的网站上制作动画效果,当我们点击菜单 link(例如,关于部分)时,它会以视差样式为该 div 制作动画效果.
伙计们,如果你们知道任何 jquery 插件可以在这种情况下帮助我,请告诉我,如果你们也向我展示一个例子会更好。
查看代码获取帮助:
.Home-section {
height: 500px;
background: deepskyblue;
}
.About-section {
height: 300px;
background: deeppink;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<a href="#">Home</a>
<a href="#">About</a>
<div class="Home-section">
<h1> Hello </h1>
</div>
<div class="About-section">
<h1>Bye</h1>
</div>
所以,根据代码,我想在 About-section 上点击 link 声明 About
希望你想要这个。谢谢
// handle links with @href started with '#' only
$(document).on('click', 'a[href^="#"]', function(e) {
// target element id
var id = $(this).attr('href');
// target element
var $id = $(id);
if ($id.length === 0) {
return;
}
// prevent standard hash navigation (avoid blinking in IE)
e.preventDefault();
// top position relative to the document
var pos = $(id).offset().top - 10;
// animated top scrolling
$('body, html').animate({scrollTop: pos});
});
.Home-section {
height:500px;
background: deepskyblue;
}
.About-section {
height:300px;
background:deeppink;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#home">Home</a>
<a href="#about">About</a>
<div class="Home-section" id="home"><h1> Hello </h1>
</div>
<div class="About-section" id="about"><h1>Bye</h1>
</div>
要获得更像视差效果,您可以添加 background-attachment: fixed;
.About-section {
height: 300px;
background: url('http://lorempicsum.com/futurama/627/200/3') no-repeat;
background-size: cover;
background-attachment: fixed;
}
注意:我使用@sagar kodte JS代码,它对动画效果很好。
请试试这个
.Home-section {
height:500px;
background: deepskyblue;
}
.About-section {
height:300px;
background:deeppink;
}
a{cursor:pointer}
html代码
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("a").click(function() {
var ourclass = $(this).attr('class');
$('html, body').animate({
scrollTop: $("#"+ourclass).offset().top-10
}, 2000);
});
});
</script>
<a class="home">Home</a>
<a class="about">About</a>
<div class="Home-section" id="home"><h1> Hello </h1></div>
<div class="About-section" id="about"><h1>Bye</h1></div>
.Home-section {
height: 500px;
background: deepskyblue;
}
.About-section {
height: 300px;
background: deeppink;
}
<a href="#home">Home</a>
<a href="#about">About</a>
<div class="Home-section" id="home">
<h1> Hello </h1>
</div>
<div class="About-section" id="about">
<h1>Bye</h1>
试试这个。
我想在我的网站上制作动画效果,当我们点击菜单 link(例如,关于部分)时,它会以视差样式为该 div 制作动画效果.
伙计们,如果你们知道任何 jquery 插件可以在这种情况下帮助我,请告诉我,如果你们也向我展示一个例子会更好。
查看代码获取帮助:
.Home-section {
height: 500px;
background: deepskyblue;
}
.About-section {
height: 300px;
background: deeppink;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<a href="#">Home</a>
<a href="#">About</a>
<div class="Home-section">
<h1> Hello </h1>
</div>
<div class="About-section">
<h1>Bye</h1>
</div>
所以,根据代码,我想在 About-section 上点击 link 声明 About
希望你想要这个。谢谢
// handle links with @href started with '#' only
$(document).on('click', 'a[href^="#"]', function(e) {
// target element id
var id = $(this).attr('href');
// target element
var $id = $(id);
if ($id.length === 0) {
return;
}
// prevent standard hash navigation (avoid blinking in IE)
e.preventDefault();
// top position relative to the document
var pos = $(id).offset().top - 10;
// animated top scrolling
$('body, html').animate({scrollTop: pos});
});
.Home-section {
height:500px;
background: deepskyblue;
}
.About-section {
height:300px;
background:deeppink;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#home">Home</a>
<a href="#about">About</a>
<div class="Home-section" id="home"><h1> Hello </h1>
</div>
<div class="About-section" id="about"><h1>Bye</h1>
</div>
要获得更像视差效果,您可以添加 background-attachment: fixed;
.About-section {
height: 300px;
background: url('http://lorempicsum.com/futurama/627/200/3') no-repeat;
background-size: cover;
background-attachment: fixed;
}
注意:我使用@sagar kodte JS代码,它对动画效果很好。
请试试这个
.Home-section {
height:500px;
background: deepskyblue;
}
.About-section {
height:300px;
background:deeppink;
}
a{cursor:pointer}
html代码
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("a").click(function() {
var ourclass = $(this).attr('class');
$('html, body').animate({
scrollTop: $("#"+ourclass).offset().top-10
}, 2000);
});
});
</script>
<a class="home">Home</a>
<a class="about">About</a>
<div class="Home-section" id="home"><h1> Hello </h1></div>
<div class="About-section" id="about"><h1>Bye</h1></div>
.Home-section {
height: 500px;
background: deepskyblue;
}
.About-section {
height: 300px;
background: deeppink;
}
<a href="#home">Home</a>
<a href="#about">About</a>
<div class="Home-section" id="home">
<h1> Hello </h1>
</div>
<div class="About-section" id="about">
<h1>Bye</h1>
试试这个。