有沒有辦法使用jQuery向下滾動到錨鏈接?jquery平滑滾動到錨點?
像:
$(document).ready(function(){
$("#gotomyanchor").click(function(){
$.scrollSmoothTo($("#myanchor"));
});
});
?
有沒有辦法使用jQuery向下滾動到錨鏈接?jquery平滑滾動到錨點?
像:
$(document).ready(function(){
$("#gotomyanchor").click(function(){
$.scrollSmoothTo($("#myanchor"));
});
});
?
下面是我該怎麼做:
var hashTagActive = "";
$(".scroll").on("click touchstart" , function (event) {
if(hashTagActive != this.hash) { //this will prevent if the user click several times the same link to freeze the scroll.
event.preventDefault();
//calculate destination place
var dest = 0;
if ($(this.hash).offset().top > $(document).height() - $(window).height()) {
dest = $(document).height() - $(window).height();
} else {
dest = $(this.hash).offset().top;
}
//go to destination
$('html,body').animate({
scrollTop: dest
}, 2000, 'swing');
hashTagActive = this.hash;
}
});
然後你只需要創建你的錨是這樣的:
<a class="scroll" href="#destination1">Destination 1</a>
你可以看到它在我的website。
的演示,也可以在這裏找到:http://jsfiddle.net/YtJcL/
jQuery.scrollTo將做你想要的一切和更多!
你可以傳遞各種不同的東西:
這是一個插件嗎?因爲我試過了,沒有發生任何事..我用這個: $ .scrollTo(「#addNewUA」,800,{easing:'elasout'}); – dynamic 2010-11-16 19:55:59
是的,它是一個插件,所以你需要下載幷包含它。雖然它非常輕巧,功能也非常棒。 (注意:我沒有任何個人聯繫,除了在很多網站中使用它) – 2010-11-16 20:03:30
我會用從CSS-Tricks.com簡單的代碼片段:
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
如果您希望所有頁面內錨鏈接都可以滾動生成而不需要任何裝飾等,這可能是更好的解決方案。否則,jQuery.scrollTo有很多很酷的其他技巧(檢查他們的演示頁面) – 2011-12-16 15:57:06
這是一個非常棒的插入式解決方案 – 2015-03-06 04:35:52
原則上 - 使用**代碼**不**鏈接** - http://meta.stackexchange.com/ questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers – 2015-08-04 11:35:49
這是我用來快速綁定jQuery的scrollTo所有錨鏈接的代碼:
// Smooth scroll
$('a[href*=#]').click(function() {
var hash = $(this).attr('href');
hash = hash.slice(hash.indexOf('#') + 1);
$.scrollTo(hash == 'top' ? 0 : 'a[name='+hash+']', 500);
window.location.hash = '#' + hash;
return false;
});
看起來您可能需要使用jQuery scrollTo插件才能使用此代碼? – 2014-04-15 06:12:29
最好的解決辦法: jQuery: Smooth Scrolling Internal Anchor Links
HTML:
<a href="#comments" class="scroll">Scroll to comments</a>
腳本:
jQuery(document).ready(function($) {
$(".scroll").click(function(event){
event.preventDefault();
$('html,body').animate({scrollTop:$(this.hash).offset().top}, 500);
});
});
這確實是最簡單的解決方案,再加上您想要平滑滾動的任何未來鏈接,只需將「滾動」類添加到鏈接即可。 – Duncanmoo 2013-05-25 11:45:40
我用我的網站是這樣的:
$(document).ready(function(){
$('a[href^="#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, 1200, 'swing', function() {
window.location.hash = target;
});
});
});
你可以改變滾動的速度,改變我默認使用的「1200」,它在大多數瀏覽器上工作得很好。
把你的頁面的<head> </head>
標籤之間的代碼之後,您需要在您的<body>
標籤來創建內部鏈接:
<a href="#home">Go to Home</a>
希望它能幫助!
PS:不要忘記調用:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
我使用的插件平滑滾動,在http://plugins.jquery.com/smooth-scroll/。有了這個插件,所有你需要包括一個鏈接到jQuery和到插件代碼:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="javascript/smoothscroll.js"></script>
(鏈接需要有類smoothScroll
工作)。
Smooth Scroll的另一個功能是,ancor名稱不顯示在URL中!
使用hanoo's script我創建了一個jQuery函數:
$.fn.scrollIntoView = function(duration, easing) {
var dest = 0;
if (this.offset().top > $(document).height() - $(window).height()) {
dest = $(document).height() - $(window).height();
} else {
dest = this.offset().top;
}
$('html,body').animate({
scrollTop: dest
}, duration, easing);
return this;
};
用法:
$('#myelement').scrollIntoView();
默認的持續時間和寬鬆的400ms的時間 「搖擺」。
工作
$('a[href*=#]').each(function() {
$(this).attr('href', $(this).attr('href').replace('#', '#_'));
$(this).on("click", function() {
var hashname = $(this).attr('href').replace('#_', '');
if($(this).attr('href') == "#_") {
$('html, body').animate({ scrollTop: 0 }, 300);
}
else {
var target = $('a[name="' + hashname + '"], #' + hashname),
targetOffset = target.offset().top;
if(targetOffset >= 1) {
$('html, body').animate({ scrollTop: targetOffset-60 }, 300);
}
}
});
});
我討厭添加功能,命名類我的代碼,所以我把這個在一起而不是。如果我停止使用平滑滾動,我會覺得有必要通過我的代碼,並刪除所有class =「scroll」的東西。使用這種技術,我可以註釋掉5行JS,並且整個網站都會更新。 :)
<a href="/about">Smooth</a><!-- will never trigger the function -->
<a href="#contact">Smooth</a><!-- but he will -->
...
...
<div id="contact">...</div>
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
// Smooth scrolling to element IDs
$('a[href^=#]:not([href=#])').on('click', function() {
var element = $($(this).attr('href'));
$('html,body').animate({ scrollTop: element.offset().top },'normal', 'swing');
return false;
});
</script>
要求:
1. <a>
元素必須具有與#
開始href屬性,並不僅僅是#
2.頁面上的元素與匹配id
屬性
它做什麼:
1.該函數使用href值創建對象anchorID
- 在該示例中,這是$('#contact')
,開始於/
2. HTML
,和BODY
是動畫頂端的anchorID
偏移 - 速度= '正常'( '快',「慢',毫秒)
- easing ='swing'('linear'等... google easing)
3。return false
- 它可以防止瀏覽器在URL中顯示哈希值
- 該腳本沒有它,但它不是「平滑」。
試試這個。這是我修改的CSS技巧代碼,它非常簡單直接,並且可以進行水平和垂直滾動。需要JQuery。這裏是一個demo
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top-10, scrollLeft:target.offset().left-10
}, 1000);
return false;
}
}
});
});
我想,對於<a href="#my-id">
工作和版本<a href="/page#my-id">
<script>
$('a[href*=#]:not([href=#])').on('click', function (event) {
event.preventDefault();
var element = $(this.hash);
$('html,body').animate({ scrollTop: element.offset().top },'normal', 'swing');
});
</script>
這爲我工作:https://css-tricks.com/snippets/jquery/smooth-scrolling/ – maahd 2015-06-18 14:41:59