[英]Change Var to 0 after button click
在我的JavaScript中,我將自動滾動頻率設置為var speed1。單擊按鈕時,我想將var spsp設置為0。 有人可以幫助我嗎?
按鍵:
<button name="NONAUTO" value="">Turn off Autoscroll</button>
這是我的Javascript:
var speed=1;
var currentpos=0,alt=1,curpos1=0,curpos2=-1;
function initialize(){
startit();
}
function scrollwindow(){
if (document.all && !document.getElementById)
temp=document.body.scrollTop;
else
temp=window.pageYOffset;
if (alt==0)
alt=0;
else
alt=0;
if (alt==0)
curpos1=temp;
else
curpos2=temp;
if (curpos1!=curpos2){
if (document.all)
currentpos=document.body.scrollTop+speed;
else
currentpos=window.pageYOffset+speed;
window.scroll(0,currentpos);
}
else {
currentpos=0;
window.scroll(0,currentpos);
}
}
function startit(){
setInterval("scrollwindow()",50);
}
window.onload=initialize;
自動滾動是可中斷的,您可以自由滾動,但是如果可以解決,請滾動並等待20秒鍾,然后進行自動滾動,那將是很棒的事情!但這只是一個額外的操作。
向按鈕添加onclick屬性,例如onclick = change()
。
並且,您可以執行以下change()
函數:
function change(){ start = 0;}
您還可以通過document.getElementsByName("NONAUTO")[0].addEventListener("click",change,false)
添加onclick事件偵聽器
你可以這樣
<button onClick = "turnOff(event)" name="NONAUTO" value="">Turn off Autoscroll</button>
在腳本文件中添加此功能。
var turnOff = function(event)
{
event.preventDefault();
speed = 0;
alert(speed);
}
如果您使用的是jQuery,則可以使用:
<button id="buttId" name="NONAUTO" value="">Turn off Autoscroll</button>
$( "#buttId" ).click( function()
{
speed = 0;
}
或如果您不想添加ID屬性:
$( "[name='NONAUTO']" ).click( function()
{
speed = 0;
}
為此,您需要在JavaScript
使用Event Handlers
。 您特別需要處理特定button
的Click事件。 您可以像其他提到的那樣在html
代碼本身中執行此操作。 這稱為Obtrusive JavaScript 。 通常建議使用Unobtrusive JavaScript 。 這是使用事件監聽器進行操作的一種方法:
window.onload = function() { var speed = 1; var button = document.getElementById("nonAuto"); button.addEventListener("click", function() { speed = 0; alert(speed); }); };
<button name="NONAUTO" id="nonAuto" value="">Turn off Autoscroll</button>
您還可以通過以下方式切換自動滾動值:
window.onload = function() { var speed = 1; var button = document.getElementById("nonAuto"); button.addEventListener("click", function() { if (speed === 1) { speed = 0; button.firstChild.data = "Turn on AutoScroll"; alert(speed); } else { speed = 1; button.firstChild.data = "Turn off AutoScroll"; alert(speed); } }); };
<button name="NONAUTO" id="nonAuto" value="">Turn off Autoscroll</button>
希望這可以幫助!!!
您想在單擊按鈕時將變量速度從1更改為0。 只需將其寫在您的html文件中:
<button onClick:'var speed = 0'></button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.