我正在设计一个网站,该网站在同一索引文件中有4个页面,每次只有一个页面处于活动状态。我的主页有3个链接。当我点击主页链接时,我需要滚动到另一个页面中的特定div。我试图实现这一点,但不是滚动到特定的div,它只显示该部分。单击链接时滚动到其他部分的特定div
0
A
回答
1
使用jQuery
$("#link").click(function(){ //id of the link which is being clicked
$('html, body').animate({
scrollTop: $("#div").offset().top //id of div to be scrolled
}, 1000);
});
当您单击DIV的部分滚动到屏幕
2
我认为你正在寻找这样的事情,与片段全屏运行的顶部的链接,检查这是你所需要的
body {
float: left;
width: 100%;
padding-bottom: 0px;
}
.common {
\t width: 100%;
\t float: left;
\t height: 100vh;
\t display: table;
}
.allbody {
\t float: left;
\t width: 100%;
}
a {
\t display: inline-block;
\t padding: 15px;
}
header {
\t float: left;
\t width: 100%;
\t position: fixed;
\t top: 0;
\t left: 0;
\t background: #fff;
}
.common h2 {
\t font-size: 30px;
\t color: #fff;
\t text-align: center;
\t padding-top: 10%;
\t display: table-cell;
\t vertical-align: middle;
}
#firstDestination {
\t background: #000;
}
#secondDestination {
\t background: #999;
}
#thirdDestination {
\t background: #ccc;
}
#fourthDestination {
\t background: #c1c1c1;
}
<body>
<header>
\t <a href="#firstDestination">first page</a>
\t <a href="#secondDestination" >second page</a>
\t <a href="#thirdDestination">third page</a>
\t <a href="#fourthDestination">fourth page</a>
</header>
<div class="allbody">
\t <div class="common" id="firstDestination" ><h2>First Page</h2></div>
\t <div class="common" id="secondDestination"><h2>Second Page</h2></div>
\t <div class="common" id="thirdDestination" ><h2>Third Page</h2></div>
\t <div class="common" id="fourthDestination" ><h2>Fourth Page</h2></div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
function isElementInViewport (el) {
//special bonus for those using jQuery
if (typeof jQuery === "function" && el instanceof jQuery) {
el = el[0];
}
var rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
);
}
// click-to-scroll behavior
$("a").click(function (e) {
e.preventDefault();
var section = this.href;
var sectionClean = section.substring(section.indexOf("#"));
$("html, body").animate({
scrollTop: $(sectionClean).offset().top
}, 1000, function() {
window.location.hash = sectionClean;
});
});
// listen for the scroll event
$(document).on("scroll", function() {
//console.log("onscroll event fired...");
// check if the anchor elements are visible
$(".common").each(function (idx, el) {
if (isElementInViewport(el)) {
// update the URL hash
if (window.history.pushState) {
var urlHash = "#" + $(el).attr("id");
\t \t \t window.history.pushState(null, null, urlHash);
}
}
});
});
</script>
0
您可以使用jQuery.animate
功能并使用scrolltop
属性。
我已经为你写下了一个例子。
$("#button").click(function() {
$('html, body').animate({
scrollTop: $("#myDiv").offset().top
}, 2000);
});
.div {
min-height: 1100px;
background: red;
}
#myDiv {
height: 100px;
background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="button">button</div>
<div class="div">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has
a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search
for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). Contrary to popular belief, Lorem Ipsum is not simply random
text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a
Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero,
written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has
a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search
for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). Contrary to popular belief, Lorem Ipsum is not simply random
text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a
Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero,
written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has
a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search
for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). Contrary to popular belief, Lorem Ipsum is not simply random
text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a
Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero,
written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has
a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search
for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). Contrary to popular belief, Lorem Ipsum is not simply random
text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a
Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero,
written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
</div>
<div id="myDiv"></div>
相关问题
- 1. 使用MaintainScrollPositionOnPostBack单击链接按钮时,滚动到页面的特定部分
- 2. UIWebView当点击其他链接时滚动到顶部
- 3. div单击href链接时不滚动
- 4. 点击链接滚动到特定部分react.js
- 5. chart.js - 点击图表中的特定部分时链接到其他页面
- 6. 单击atag时显示特定div。并隐藏其他div
- 7. 滚动到网页的其他部分
- 8. 单击链接时滚动到页面底部?
- 9. 点击DIV时滚动到顶部?
- 10. 将onclick事件动态添加到特定div和其他特定格式链接中的链接
- 11. JQuery在单击链接时选择div中的特定值
- 12. 滚动时重复其他部分值的单元
- 13. 单击特定元素时创建高亮部分,并在不活动时隐藏其他部分
- 14. 滚动到JFrame的特定部分
- 15. 链接到网页的特定部分
- 16. 当TD被点击时,可滚动的DIV滚动到顶部
- 17. jQuery单页滚动链接到其他页面
- 18. (div滚动跟随)当div遇到anothere div时,(div滚动跟随)也滚动到其他div
- 19. 使用webview直接滚动到android活动的特定部分
- 20. 滚动到特定div
- 21. 没有其他div滚动时滚动div
- 22. UICollectionView滚动到特定部分
- 23. 当单击原始div时输出一个可滚动的div,并自动滚动到div的底部
- 24. 当点击链接时,防止网页滚动到顶部
- 25. 点击#链接时,如何让页面不滚动到顶部?
- 26. 如何通过使用完整page.js的链接滚动到特定部分?
- 27. 单击外部链接后链接到页面的特定位置
- 28. jQuery点击div的特定部分
- 29. 使用jQuery创建DIV并将其各部分链接到其他Jquery函数
- 30. 点击链接,关闭div,用动画打开其他
链接的href属性应指向您要滚动到div的ID。 –
你的问题是关于它不滚动或者它没有动画滚动的问题,因为在你看不到它滚动? –
@jijishthomas你检查了答案 –