简体   繁体   English

单击按钮后将Var更改为0

[英]Change Var to 0 after button click

In my javascript I have an autoscroll wich is set to var speed 1. I want to set the var spped to 0 when there is clicked on a button. 在我的JavaScript中,我将自动滚动频率设置为var speed1。单击按钮时,我想将var spsp设置为0。 Somebody out there who can help me? 有人可以帮助我吗?

Button: 按键:

<button name="NONAUTO"  value="">Turn off Autoscroll</button>

Here's my Javascript: 这是我的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;

The autoscroll is interruptable, you can free scroll but if you can fix if you scroll and wait like 20 seconds and thenproceed the autoscroll that would be great!But this is just an extra. 自动滚动是可中断的,您可以自由滚动,但是如果可以解决,请滚动并等待20秒钟,然后进行自动滚动,那将是很棒的事情!但这只是一个额外的操作。

Add an onclick attribute to the button like onclick = change() . 向按钮添加onclick属性,例如onclick = change()

And, you can make the following change() function : 并且,您可以执行以下change()函数:

function change(){ start = 0;}

You can also add onclick event listener by document.getElementsByName("NONAUTO")[0].addEventListener("click",change,false) 您还可以通过document.getElementsByName("NONAUTO")[0].addEventListener("click",change,false)添加onclick事件侦听器

you can do it like this. 你可以这样

 <button onClick = "turnOff(event)" name="NONAUTO"  value="">Turn off Autoscroll</button>

in the script file add this function. 在脚本文件中添加此功能。

var turnOff = function(event)
{
  event.preventDefault();
  speed = 0;
  alert(speed);
}

If you are using jQuery you could use: 如果您使用的是jQuery,则可以使用:

<button id="buttId" name="NONAUTO"  value="">Turn off Autoscroll</button>
$( "#buttId" ).click( function()
{
  speed = 0;
}

or if you don't want to add an ID attribute: 或如果您不想添加ID属性:

$( "[name='NONAUTO']" ).click( function()
{
  speed = 0;
}

To do this, you need to use Event Handlers in JavaScript . 为此,您需要在JavaScript使用Event Handlers You specifically need to handle the Click event of the particular button . 您特别需要处理特定buttonClick事件。 You can do this in the html code itself like other have mentioned. 您可以像其他提到的那样在html代码本身中执行此操作。 This is called Obtrusive JavaScript . 这称为Obtrusive JavaScript It is generally recommended to use Unobtrusive JavaScript . 通常建议使用Unobtrusive JavaScript Here's one way you can do it using the Event Listeners : 这是使用事件监听器进行操作的一种方法:

 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> 

You can also toggle the autoscroll value in this way : 您还可以通过以下方式切换自动滚动值:

 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> 

Hope this helps!!! 希望这可以帮助!!!

You want to change the variable speed, from 1 to 0 when a button is clicked. 您想在单击按钮时将变量速度从1更改为0。 Simply just write this in your html file: 只需将其写在您的html文件中:

<button onClick:'var speed = 0'></button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM