⑴ js如何實現慣性滑動效果
主要思路是:滑鼠當前點到下一點直接間隔計算出速度。這樣就實現了慣性滑動效果。
下面是簡單的js代碼實現:僅供參考:
<style>
#div1{width:100px;height:100px;background:red;position:absolute;left:0px;top:0;}
</style>
<script>
window.onload=function(){
varoDiv=document.getElementById('div1');
variSpeedX=0;
variSpeedY=0;
varlastX=0;
varlastY=0;
vartimer=null;
oDiv.onmousedown=function(ev){//div的滑鼠按下事件,主要計算滑鼠當前位置,和移動位置。這樣可以計算出滑鼠移動速度。
varoEvent=ev||event;
vardisX=oEvent.clientX-oDiv.offsetLeft;
vardisY=oEvent.clientY-oDiv.offsetTop;
clearInterval(timer);
document.onmousemove=function(ev){//滑鼠拖動事件。
varoEvent=ev||event;
oDiv.style.left=oEvent.clientX-disX+'px';
oDiv.style.top=oEvent.clientY-disY+'px';
iSpeedX=oEvent.clientX-lastX;
iSpeedY=oEvent.clientY-lastY;
lastX=oEvent.clientX;
lastY=oEvent.clientY;
}
document.onmouseup=function(){//當滑鼠抬起後,清掉移動事件。
document.onmousemove=null;
document.onmouseup=null;
oDiv.releaseCapture&&oDiv.releaseCapture();
startMove();
}
oDiv.setCapture&&oDiv.setCapture();
returnfalse;
}
functionstartMove(){//移動函數,主要操作是計算滑鼠移動速度和移動方向。
clearInterval(timer);
timer=setInterval(function(){
iSpeedY+=3;
vart=oDiv.offsetTop+iSpeedY;
varl=oDiv.offsetLeft+iSpeedX;
if(t>document.documentElement.clientHeight-oDiv.offsetHeight){
t=document.documentElement.clientHeight-oDiv.offsetHeight;
iSpeedY*=-0.8;
iSpeedX*=0.8;
}
if(t<0){
t=0;
iSpeedY*=-0.8;
iSpeedX*=0.8;
}
if(l>document.documentElement.clientWidth-oDiv.offsetWidth){
l=document.documentElement.clientWidth-oDiv.offsetWidth;
iSpeedX*=-0.8;
iSpeedY*=0.8;
}
if(l<0){
l=0;
iSpeedX*=-0.8;
iSpeedY*=0.8;
}
oDiv.style.left=l+'px';
oDiv.style.top=t+'px';
if(Math.abs(iSpeedX)<1)iSpeedX=0;
if(Math.abs(iSpeedY)<1)iSpeedY=0;
if(iSpeedX==0&&iSpeedY==0&&t==document.documentElement.clientHeight-oDiv.offsetHeight){
clearInterval(timer);
}
document.title=i++;
},30);
}
};
</script>
</head>
<body>
<divid="div1"></div>
</body>
⑵ 如何使用JS控制DIV內容的滾動條
1、首先需要新建一個HTML文檔,這里設立一下基本的架構。
⑶ js上下滾動代碼,最小化網頁再打開,就很快滾動不停。
<scripttype="text/javascript">
$(document).ready(function(){
varTop=-400;//定義一個向上移動的距離,這個數值和你圖片或DIV的高度相等
varTime=500;//定義一個速度
functionmove(){
if($("#box").is(":animated"))return;
$("#box").animate({
"margin-top":Top
},Time);//animate方法,只能對數值型的值進行漸變
Top+=-400;//運行一次增加一個圖友運片的高度
if(Top==-800)//判斷當總高度大於你DIV或者圖片總高度
{
Top=0;//把距離設置散攜回0
Time=500;//加快移動速度
}else{
Time=500;//否則減慢速度沖告伏
}
}
setInterval(move,6000);//3秒執行一次move()
})
</script>
⑷ js上下滾動出現卡頓
谷歌瀏覽器下還是很正常的啊。頃埋。沒有卡頓的。
不知道你說的卡頓是不是指在滾動到頂端的時候的跳動。。
確實跳了一下。
如果你的拆螞p標簽沒有重置margin、padding的話,可以這么改
if(demo2.offsetHeight-demo.scrollTop+16<0)//當滾動至demo1與demo2交界時
demo.scrollTop-=demo1.offsetHeight+16//demo跳到最頂端
}
16是谷歌瀏覽器下p標簽默認的margin-top的值。
這樣就不會有跳雀御螞動了。
⑸ js無限滾動中,iOS慣性滾動停止的位置怎麼確定
可以給你的webView的scrollView屬性指氏碰敗定delegate然後吵旦實現scrollViewDidEndDecelerating方法來監聽慣性滾動停止殲顫事件,這個時候獲取scrollView的滾動位置,然後通過js傳到網頁中
⑹ js實現圖片滾動效果
<SCRIPT language="JavaScript">
var speed=50; //設置滾動速度
demo2.innerHTML=demo1.innerHTML //復制dome1為dome2
function Marquee(){
if(demo2.offsetTop-demo.scrollTop<=0) //當滾動至dome1與dome2交界時
demo.scrollTop-=demo1.offsetHeight //dome跳到最頂端
else{
demo.scrollTop++
}
}
var MyMar=setInterval(Marquee,speed) //設置定時器
demo.onmouseover=function() {clearInterval(MyMar)}//滑鼠移上時清除定時器達到滾動停止的目的
demo.onmouseout=function() {MyMar=setInterval(Marquee,speed)}//滑鼠移開時重設定時器,繼續滾動
</SCRIPT>
<div id="demo" style="overflow: hidden; height: 600; width: 180; background: #214984; color: #ffffff">
<div id="demo1" align="center">
<!-- 定義圖片 -->
</div>
<div id="demo2" align="center"></div>
</div>
⑺ 如何通過JS控制DIV內容的滾動
//調整這個div的scrollTop屬性即可
vardiv=document.getElementsByTagName('div')[0];
div.scrollTop=100;
⑻ js或jquery 怎麼監聽由於屏幕慣性滾動而產生的距離
慣性滾動?手機端么?PC端沒有慣性滾動的吧。
那麼你數派手機判弊端用的什薯沖賀么?iscroll?.,不然也不會產生慣性滾動啊
⑼ JS代碼滾動,我寫好了,但它不會動,請高手幫改下!
缺包含文件啊
<!--
/*MSClass (Class Of Marquee Scroll通用不間斷滾動JS封裝類) Ver 1.65*\
right qqlxinye
應用說明:頁麵包含<script type="text/javascript" src="MSClass.js"></script>
創建實例:
//參數直接賦值法
new Marquee("marquee")
new Marquee("marquee","top")
......
new Marquee("marquee",0,1,760,52)
new Marquee("marquee","top",1,760,52,50,5000)
......
new Marquee("marquee",0,1,760,104,50,5000,3000,52)
new Marquee("marquee",null,null,760,104,null,5000,null,-1)
//參數動態賦值法
var marquee1 = new Marquee("marquee") *此參數必選
marquee1.Direction = "top"; 或者 marquee1.Direction = 0;
marquee1.Step = 1;
marquee1.Width = 760;
marquee1.Height = 52;
marquee1.Timer = 50;
marquee1.DelayTime = 5000;
marquee1.WaitTime = 3000;
marquee1.ScrollStep = 52;
marquee1.Start();
參數說明:
ID "marquee" 容器ID (必選)
Direction (0) 滾動方向 (可選,默認為0向上滾動) 可設置的值包括:0,1,2,3,"top","bottom","left","right" (0向上 1向下 2向左 3向右)
Step (1) 滾動的步長 (可選,默認值為2,數值越大,滾動越快)
Width (760) 容器可視寬度 (可選,默認值為容器初始設置的寬度)
Height (52) 容器可視高度 (可選,默認值為容器初始設置的高度)
Timer (50) 定時器 (可選,默認值為30,數值越小,滾動的速度越快,1000=1秒,建議不小於20)
DelayTime (5000) 間歇停頓延遲時間(可選,默認為0不停頓,1000=1秒)
WaitTime (3000) 開始時的等待時間(可選,默認或0為不等待,1000=1秒)
ScrollStep (52) 間歇滾動間距 (可選,默認為翻屏寬/高度,該數值與延遲均為0則為滑鼠懸停控制,-1禁止滑鼠控制)
使用建議:
1、建議直接賦予容器的顯示區域的寬度和高度,如(<div id="marquee" style="width:760px;height:52px;">......</div>)
2、建議為容器添加樣式overflow = auto,如(<div id="marquee" style="width:760px;height:52px;overflow:auto;">......</div>)
3、為了更准確的獲取滾動區域的寬度和高度,請盡可能將各滾動單位直接賦予正確寬高度
4、對於TABLE標記的橫向滾動,需要對TABLE添加樣式display = inline,如(<div id="marquee" style="width:760px;height:52px;overflow:auto;"><table style="display:inline">......</table></div>)
5、對於翻屏滾動或間歇滾動,要注意各滾動單位間的間距,同時需要對容器的可視高度和可視寬度做好准確的設置,對於各滾動單位間的間距可以通過設置行間距或者單元格的高寬度來進行調整
6、對於LI自動換行的問題暫時沒有更好的解決辦法,建議將其轉換成表格(TABLE)的形式來達到同等的效果
7、針對橫向滾動的文欄位落,如果最末端是以空格" "結束的,請將空格" "轉換成" "
8、滑鼠懸停滾動思想源自Flash,所以有一定的局限性(容器內僅允許用圖片<img>或者帶鏈接的圖片<a><img></a>的形式,並需要禁止其自動換行)
\***程序製作/版權所有:qqlxinye.cn
function Marquee()
{
this.ID = document.getElementById(arguments[0]);
if(!this.ID)
{
alert("您要設置的\"" + arguments[0] + "\"初始化錯誤\r\n請檢查標簽ID設置是否正確!");
this.ID = -1;
return;
}
this.Direction = this.Width = this.Height = this.DelayTime = this.WaitTime = this.CTL = this.StartID = this.Stop = this.MouseOver = 0;
this.Step = 1;
this.Timer = 30;
this.DirectionArray = {"top":0 , "up":0 , "bottom":1 , "down":1 , "left":2 , "right":3};
if(typeof arguments[1] == "number" || typeof arguments[1] == "string")this.Direction = arguments[1];
if(typeof arguments[2] == "number")this.Step = arguments[2];
if(typeof arguments[3] == "number")this.Width = arguments[3];
if(typeof arguments[4] == "number")this.Height = arguments[4];
if(typeof arguments[5] == "number")this.Timer = arguments[5];
if(typeof arguments[6] == "number")this.DelayTime = arguments[6];
if(typeof arguments[7] == "number")this.WaitTime = arguments[7];
if(typeof arguments[8] == "number")this.ScrollStep = arguments[8];
this.ID.style.overflow = this.ID.style.overflowX = this.ID.style.overflowY = "hidden";
this.ID.noWrap = true;
this.IsNotOpera = (navigator.userAgent.toLowerCase().indexOf("opera") == -1);
if(arguments.length >= 7)this.Start();
}
Marquee.prototype.Start = function()
{
if(this.ID == -1)return;
if(this.WaitTime < 800)this.WaitTime = 800;
if(this.Timer < 20)this.Timer = 20;
if(this.Width == 0)this.Width = parseInt(this.ID.style.width);
if(this.Height == 0)this.Height = parseInt(this.ID.style.height);
if(typeof this.Direction == "string")this.Direction = this.DirectionArray[this.Direction.toString().toLowerCase()];
this.HalfWidth = Math.round(this.Width / 2);
this.HalfHeight = Math.round(this.Height / 2);
this.BakStep = this.Step;
this.ID.style.width = this.Width + "px";
this.ID.style.height = this.Height + "px";
if(typeof this.ScrollStep != "number")this.ScrollStep = this.Direction > 1 ? this.Width : this.Height;
var templateLeft = "<table cellspacing='0' cellpadding='0' style='border-collapse:collapse;display:inline;'><tr><td noWrap=true style='white-space: nowrap;word-break:keep-all;'>MSCLASS_TEMP_HTML</td><td noWrap=true style='white-space: nowrap;word-break:keep-all;'>MSCLASS_TEMP_HTML</td></tr></table>";
var templateTop = "<table cellspacing='0' cellpadding='0' style='border-collapse:collapse;'><tr><td>MSCLASS_TEMP_HTML</td></tr><tr><td>MSCLASS_TEMP_HTML</td></tr></table>";
var msobj = this;
msobj.tempHTML = msobj.ID.innerHTML;
if(msobj.Direction <= 1)
{
msobj.ID.innerHTML = templateTop.replace(/MSCLASS_TEMP_HTML/g,msobj.ID.innerHTML);
}
else
{
if(msobj.ScrollStep == 0 && msobj.DelayTime == 0)
{
msobj.ID.innerHTML += msobj.ID.innerHTML;
}
else
{
msobj.ID.innerHTML = templateLeft.replace(/MSCLASS_TEMP_HTML/g,msobj.ID.innerHTML);
}
}
var timer = this.Timer;
var delaytime = this.DelayTime;
var waittime = this.WaitTime;
msobj.StartID = function(){msobj.Scroll()}
msobj.Continue = function()
{
if(msobj.MouseOver == 1)
{
setTimeout(msobj.Continue,delaytime);
}
else
{ clearInterval(msobj.TimerID);
msobj.CTL = msobj.Stop = 0;
msobj.TimerID = setInterval(msobj.StartID,timer);
}
}
msobj.Pause = function()
{
msobj.Stop = 1;
clearInterval(msobj.TimerID);
setTimeout(msobj.Continue,delaytime);
}
msobj.Begin = function()
{
msobj.ClientScroll = msobj.Direction > 1 ? msobj.ID.scrollWidth / 2 : msobj.ID.scrollHeight / 2;
if((msobj.Direction <= 1 && msobj.ClientScroll <= msobj.Height + msobj.Step) || (msobj.Direction > 1 && msobj.ClientScroll <= msobj.Width + msobj.Step)) {
msobj.ID.innerHTML = msobj.tempHTML;
delete(msobj.tempHTML);
return;
}
delete(msobj.tempHTML);
msobj.TimerID = setInterval(msobj.StartID,timer);
if(msobj.ScrollStep < 0)return;
msobj.ID.onmousemove = function(event)
{
if(msobj.ScrollStep == 0 && msobj.Direction > 1)
{
var event = event || window.event;
if(window.event)
{
if(msobj.IsNotOpera)
{
msobj.EventLeft = event.srcElement.id == msobj.ID.id ? event.offsetX - msobj.ID.scrollLeft : event.srcElement.offsetLeft - msobj.ID.scrollLeft + event.offsetX;
}
else
{
msobj.ScrollStep = null;
return;
}
}
else
{
msobj.EventLeft = event.layerX - msobj.ID.scrollLeft;
}
msobj.Direction = msobj.EventLeft > msobj.HalfWidth ? 3 : 2;
msobj.AbsCenter = Math.abs(msobj.HalfWidth - msobj.EventLeft);
msobj.Step = Math.round(msobj.AbsCenter * (msobj.BakStep*2) / msobj.HalfWidth);
}
}
msobj.ID.onmouseover = function()
{
if(msobj.ScrollStep == 0)return;
msobj.MouseOver = 1;
clearInterval(msobj.TimerID);
}
msobj.ID.onmouseout = function()
{
if(msobj.ScrollStep == 0)
{
if(msobj.Step == 0)msobj.Step = 1;
return;
}
msobj.MouseOver = 0;
if(msobj.Stop == 0)
{
clearInterval(msobj.TimerID);
msobj.TimerID = setInterval(msobj.StartID,timer);
}
}
}
setTimeout(msobj.Begin,waittime);
}
Marquee.prototype.Scroll = function()
{
switch(this.Direction)
{
case 0:
this.CTL += this.Step;
if(this.CTL >= this.ScrollStep && this.DelayTime > 0)
{
this.ID.scrollTop += this.ScrollStep + this.Step - this.CTL;
this.Pause();
return;
}
else
{
if(this.ID.scrollTop >= this.ClientScroll)
{
this.ID.scrollTop -= this.ClientScroll;
}
this.ID.scrollTop += this.Step;
}
break;
case 1:
this.CTL += this.Step;
if(this.CTL >= this.ScrollStep && this.DelayTime > 0)
{
this.ID.scrollTop -= this.ScrollStep + this.Step - this.CTL;
this.Pause();
return;
}
else
{
if(this.ID.scrollTop <= 0)
{
this.ID.scrollTop += this.ClientScroll;
}
this.ID.scrollTop -= this.Step;
}
break;
case 2:
this.CTL += this.Step;
if(this.CTL >= this.ScrollStep && this.DelayTime > 0)
{
this.ID.scrollLeft += this.ScrollStep + this.Step - this.CTL;
this.Pause();
return;
}
else
{
if(this.ID.scrollLeft >= this.ClientScroll)
{
this.ID.scrollLeft -= this.ClientScroll;
}
this.ID.scrollLeft += this.Step;
}
break;
case 3:
this.CTL += this.Step;
if(this.CTL >= this.ScrollStep && this.DelayTime > 0)
{
this.ID.scrollLeft -= this.ScrollStep + this.Step - this.CTL;
this.Pause();
return;
}
else
{
if(this.ID.scrollLeft <= 0)
{
this.ID.scrollLeft += this.ClientScroll;
}
this.ID.scrollLeft -= this.Step;
}
break;
}
}
//-->
⑽ JS - 滾動事件(onscroll)
當瀏覽器滾動條滾動時會觸發 onscroll 事件,通常情況下指上下滾動條滾動。
需要注意的是,瀏覽器一旦檢測到滾動條發生滾動,就可以觸發 onscroll 事件,而無需等到滾動行為結束。
下面的例子演示了一個極普遍的應用:利用 onscroll 事件檢測滾動條位置,當向下滾動到一定位置時,在頁面右下角會出現「返回頂部」的錨鏈接,以方便網站用戶快速返回頂部。