javascript - scroll底部 - js置頂
jQuery滾動到元素 (16)
我有這個input
元素:
<input type="text" class="textfield" value="" id="subject" name="subject">
然後我還有一些其他元素,如其他文本輸入,textareas等。
當用戶用#subject
單擊該input
時,頁面應該以良好的動畫滾動到頁面的最後一個元素。 它應該是一個滾動到底部而不是頂部。
該頁面的最後一個項目是一個submit
按鈕,其中包含#submit
:
<input type="submit" class="submit" id="submit" name="submit" value="Ok, Done.">
動畫不應該太快,應該是流暢的。
我正在運行最新的jQuery版本。 我更喜歡不安裝任何插件,而是使用默認的jQuery功能來實現此目的。
當用戶用#subject單擊該輸入時,頁面應該以良好的動畫滾動到頁面的最後一個元素。 它應該是一個滾動到底部而不是頂部。
該頁面的最後一項是一個帶#submit的提交按鈕
$('#subject').click(function()
{
$('#submit').focus();
$('#subject').focus();
});
這將首先向下滾動到#submit
然後將光標恢復到被單擊的輸入,該輸入模仿向下滾動,並適用於大多數瀏覽器。 它也不需要jQuery,因為它可以用純JavaScript編寫。
通過鏈接focus
調用,這種使用focus
功能的方式能夠以更好的方式模擬動畫。 我沒有測試過這個理論,但它看起來像這樣:
<style>
#F > *
{
width: 100%;
}
</style>
<form id="F" >
<div id="child_1"> .. </div>
<div id="child_2"> .. </div>
..
<div id="child_K"> .. </div>
</form>
<script>
$('#child_N').click(function()
{
$('#child_N').focus();
$('#child_N+1').focus();
..
$('#child_K').focus();
$('#child_N').focus();
});
</script>
Steve和Peter的解決方案非常好。
但在某些情況下,您可能必須將該值轉換為整數。 奇怪的是,從$("...").offset().top
返回的值有時是float
。
使用: parseInt($("....").offset().top)
例如:
$("#button").click(function() {
$('html, body').animate({
scrollTop: parseInt($("#elementtoScrollToID").offset().top)
}, 2000);
});
$('html, body').animate(...)
不適合iphone,android chrome Safari瀏覽器。
我必須定位頁面的根內容元素。
$('#cotnent')。動畫(...)
這是我結束了
if (navigator.userAgent.match(/(iPod|iPhone|iPad|Android)/)) {
$('#content').animate({
scrollTop: $("#elementtoScrollToID").offset().top
}, 'slow');
}
else{
$('html, body').animate({
scrollTop: $("#elementtoScrollToID").offset().top
}, 'slow');
}
所有身體內容都與#content div聯繫起來
<html>
....
<body>
<div id="content">
....
</div>
</body>
</html>
使用此解決方案,您不需要任何插件,除了在關閉</body>
標籤之前放置腳本之外,不需要任何安裝程序 。
$("a[href^='#']").on("click", function(e) {
e.preventDefault();
$("html, body").animate({
scrollTop: $($(this).attr("href")).offset().top
}, 1000);
});
if ($(window.location.hash).length > 1) {
$("html, body").animate({
scrollTop: $(window.location.hash).offset().top
}, 1000);
}
在加載時,如果地址中存在散列,我們將滾動到該地址。
而且 - 無論何時您點擊帶有href
散列a
鏈接(例如#top
,我們都會滾動到該鏈接。
假設你有一個帶有id button
,試試這個例子:
$("#button").click(function() {
$('html, body').animate({
scrollTop: $("#elementtoScrollToID").offset().top
}, 2000);
});
我從文章中得到了代碼順利滾動到沒有jQuery插件的元素 。 我已經在下面的例子中對它進行了測試。
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script>
$(document).ready(function (){
$("#click").click(function (){
$('html, body').animate({
scrollTop: $("#div1").offset().top
}, 2000);
});
});
</script>
<div id="div1" style="height: 1000px; width 100px">
Test
</div>
<br/>
<div id="div2" style="height: 1000px; width 100px">
Test 2
</div>
<button id="click">Click me</button>
</html>
動畫:
// slide to top of the page
$('.up').click(function () {
$("html, body").animate({
scrollTop: 0
}, 600);
return false;
});
// slide page to anchor
$('.menutop b').click(function(){
//event.preventDefault();
$('html, body').animate({
scrollTop: $( $(this).attr('href') ).offset().top
}, 600);
return false;
});
// Scroll to class, div
$("#button").click(function() {
$('html, body').animate({
scrollTop: $("#target-element").offset().top
}, 1000);
});
// div background animate
$(window).scroll(function () {
var x = $(this).scrollTop();
// freezze div background
$('.banner0').css('background-position', '0px ' + x +'px');
// from left to right
$('.banner0').css('background-position', x+'px ' +'0px');
// from right to left
$('.banner0').css('background-position', -x+'px ' +'0px');
// from bottom to top
$('#skills').css('background-position', '0px ' + -x + 'px');
// move background from top to bottom
$('.skills1').css('background-position', '0% ' + parseInt(-x / 1) + 'px' + ', 0% ' + parseInt(-x / 1) + 'px, center top');
// Show hide mtop menu
if ( x > 100 ) {
$( ".menu" ).addClass( 'menushow' );
$( ".menu" ).fadeIn("slow");
$( ".menu" ).animate({opacity: 0.75}, 500);
} else {
$( ".menu" ).removeClass( 'menushow' );
$( ".menu" ).animate({opacity: 1}, 500);
}
});
// progres bar animation simple
$('.bar1').each(function(i) {
var width = $(this).data('width');
$(this).animate({'width' : width + '%' }, 900, function(){
// Animation complete
});
});
如果你想在一個溢出容器中滾動(而不是上面的$('html, body')
),也可以使用絕對定位,這是一種方法:
var elem = $('#myElement'),
container = $('#myScrollableContainer'),
pos = elem.position().top + container.scrollTop() - container.position().top;
container.animate({
scrollTop: pos
}
如果您只處理滾動到輸入元素,則可以使用focus()
。 例如,如果您想滾動到第一個可見輸入:
$(':input:visible').first().focus();
或者類的.error
容器中的第一個可見輸入:
$('.error :input:visible').first().focus();
感謝Tricia Ball指出了這一點!
對於它的價值,這是我如何設法實現這種行為的一般元素,可以在DIV內滾動。 在我們的例子中,我們並沒有滾動整個主體,而是僅僅使用了溢出的特定元素:auto; 在一個更大的佈局。
它會創建目標元素高度的假輸入,然後將焦點放在該元素上,並且無論您在可滾動層次結構中有多深,瀏覽器都會關心其餘部分。 奇蹟般有效。
var $scrollTo = $('#someId'),
inputElem = $('<input type="text"></input>');
$scrollTo.prepend(inputElem);
inputElem.css({
position: 'absolute',
width: '1px',
height: $scrollTo.height()
});
inputElem.focus();
inputElem.remove();
我寫了一個通用函數,可以滾動到jQuery對象,CSS選擇器或數值。
用法示例:
// scroll to "#target-element":
$.scrollTo("#target-element");
// scroll to 80 pixels above first element with class ".invalid":
$.scrollTo(".invalid", -80);
// scroll a container with id "#my-container" to 300 pixels from its top:
$.scrollTo(300, 0, "slow", "#my-container");
該函數的代碼:
/**
* Scrolls the container to the target position minus the offset
*
* @param target - the destination to scroll to, can be a jQuery object
* jQuery selector, or numeric position
* @param offset - the offset in pixels from the target position, e.g.
* pass -80 to scroll to 80 pixels above the target
* @param speed - the scroll speed in milliseconds, or one of the
* strings "fast" or "slow". default: 500
* @param container - a jQuery object or selector for the container to
* be scrolled. default: "html, body"
*/
jQuery.scrollTo = function (target, offset, speed, container) {
if (isNaN(target)) {
if (!(target instanceof jQuery))
target = $(target);
target = parseInt(target.offset().top);
}
container = container || "html, body";
if (!(container instanceof jQuery))
container = $(container);
speed = speed || 500;
offset = offset || 0;
container.animate({
scrollTop: target + offset
}, speed);
};
查看ScrollTo插件。 你可以在看到演示。
我希望它有幫助。
簡單的方法來實現頁面的滾動目標div ID
var targetOffset = $('#divID').offset().top;
$('html, body').animate({scrollTop: targetOffset}, 1000);
要顯示完整元素(如果可以使用當前窗口大小):
var element = $("#some_element");
var elementHeight = element.height();
var windowHeight = $(window).height();
var offset = Math.min(elementHeight, windowHeight) + element.offset().top;
$('html, body').animate({ scrollTop: offset }, 500);
這是我使用泛型類選擇器抽象ID和href的方法
$(function() {
// Generic selector to be used anywhere
$(".js-scroll-to").click(function(e) {
// Get the href dynamically
var destination = $(this).attr('href');
// Prevent href=“#” link from changing the URL hash (optional)
e.preventDefault();
// Animate scroll to destination
$('html, body').animate({
scrollTop: $(destination).offset().top
}, 500);
});
});
<!-- example of a fixed nav menu -->
<ul class="nav">
<li>
<a href="#section-1" class="nav-item js-scroll-to">Item 1</a>
</li>
<li>
<a href="#section-2" class="nav-item js-scroll-to">Item 2</a>
</li>
<li>
<a href="#section-3" class="nav-item js-scroll-to">Item 3</a>
</li>
</ul>
jQuery(document).ready(function($) {
$('a[href^="#"]').bind('click.smoothscroll',function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate( {
'scrollTop': $target.offset().top-40
}, 900, 'swing', function () {
window.location.hash = target;
} );
} );
} );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul role="tablist">
<li class="active" id="p1"><a href="#pane1" role="tab">Section 1</a></li>
<li id="p2"><a href="#pane2" role="tab">Section 2</a></li>
<li id="p3"><a href="#pane3" role="tab">Section 3</a></li>
</ul>
<div id="pane1"></div>
<div id="pane2"></div>
<div id="pane3"></div>
$('html, body').animate({scrollTop:
Math.min(
$(to).offset().top-margintop, //margintop is the margin above the target
$('body')[0].scrollHeight-$('body').height()) //if the target is at the bottom
}, 2000);