[英]Moving a div relative to what vertical position you are on a scrollable page
想象一下,我有一個網站的視口高度溢出了三倍。 我可以從上至下滾動瀏覽該網站。
是否可以創建相對於已滾動的總頁面數量從視口頂部移至底部的div?
示例-有人介意幫助我實現這樣的目標嗎? 不知道如何完成。
編輯:
var scrollValue;
var percent;
var body = document.body,
html = document.documentElement;
var height = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight );
$(document).ready(function(){
$("button").click(function(){
scrollValue = $("body").scrollTop();
percent = (scrollValue / height) * 100;
alert(percent);
});
});
您可以嘗試使用jQuery scrollTop()
。 請參閱jquery.com上的演示 。
想法:如果您了解此功能的兩種使用方式(有/無args),則可以使用同一功能來獲取自定義滾動條的當前滾動位置,然后觸發div滾動到相對滾動位置。
替代任務:如果要在鼠標滾輪移動時滾動自定義滾動條,建議使用jquery鼠標滾輪插件 -
$(window).mousewheel(function(turn, delta) {
if (delta == 1) // going down
else //going up
// all kinds of code
return false;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.