Я создаю веб-сайт, который имеет 4 страницы в одном файле индекса, из которых активна только одна страница. У меня есть 3 ссылки на моей домашней странице. Мне нужно прокрутить до определенного 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. UICollectionView прокрутите список до определенного раздела
- 2. Прокрутите до раздела на клик (необходимо заполнить)
- 3. Прокрутите вниз до раздела из iframe
- 4. Прокрутите до элемента при нажатии клавиши
- 5. Прокрутите QTableWidget до определенного столбца
- 6. jQuery - Анимация прокрутите до страницы раздела
- 7. открытие определенного раздела на аккордеоне с другой ссылки на страницу
- 8. jQuery прокрутите страницу вверх и прокрутите страницу до следующего раздела.
- 9. скопируйте содержимое раздела в другой раздел, не нарушая разрывов раздела?
- 10. Калитка: прокрутите до определенного якоря после нажатия ссылки
- 11. Прокрутите вниз до раздела с помощью Javascript
- 12. HTML - прокрутите в конец div при нажатии ссылки
- 13. прокрутите до определенного содержимого в редакторе TinyMCE
- 14. UITableView прокрутите до раздела в пределах размера содержимого
- 15. Прокрутите заголовок раздела заголовка при указании раздела раздела?
- 16. Прокрутите до определенной позиции сверху окна при нажатии
- 17. jQuery swap logo при нажатии ссылки и достижении определенного раздела страницы
- 18. Использование jQuery в навигации при нажатии ссылки - перейти на целевую страницу, затем прокрутить до определенного раздела страницы
- 19. Прокрутите до раздела Добавить класс css в прогресс bar jquery
- 20. Убрать из списка другой раздел из раздела
- 21. Android: прокрутите до определенного вида в ListView при нажатии на выбранный вид
- 22. jquery прокрутите до именованного якоря при навигации с другой страницы
- 23. Прокрутите страницу вверх в iframe при нажатии
- 24. calabash-ios UIPickerView прокрутите до определенного значения
- 25. Прокрутите до определенного div с url
- 26. Прокрутите список до определенного DIV - jQuery
- 27. ScrollViewer прокрутите до определенного элемента управления
- 28. Прокрутите div 30px при нажатии на привязку
- 29. Показать jQuery Аккордеон при нажатии ссылки?
- 30. Обновление ссылки при нажатии другой ссылки с помощью jquery
атрибут ссылки ссылки должны указывать на идентификатор DIV вы хотите, чтобы перейти к. –
Есть ли у вас вопрос относительно того, что он не прокручивается или что он не прокручивается анимированно, так как вы не видите его прокрутку? –
@jijishthomas вы проверили ответ –