【问题标题】:JavaScript variable won't increase with var++JavaScript 变量不会随 var++ 增加
【发布时间】:2017-02-28 00:35:47
【问题描述】:

在我创建的一个小网站上-click here 如果你想看的话。

无论如何,使用 jQuery,当你向下滚动时,你会被拉回来,顶部的文本会发生变化,并且一个变量会增加。根据变量的值,顶部的文本会相应更改。这里的问题是这个数字没有进一步改变。它只显示第一个文本,即如果变量等于 1,但不会进一步超过。这是带有一些注释的代码来表达我的意思。

FUN = 0; //Does not have "var" so it can be global to the following function
window.onscroll = function(ev) {
    if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
         FUN++; //Adds one to FUN variable.
         if(FUN==1){ //This stuff here executes just fine.
         scream.playclip(); //This also works too; don't worry.
         $("#text").replaceWith( "<h1>You should not have done that.</h1><br><img src='sck1.jpg'>" );
         } else if(FUN==2){ //This doesn't happen.
         $("#text").replaceWith( "<h1>Last chance to close the page.</h1>" );
         }
       $('html, body').animate({ scrollTop: 0 }, 'fast'); //This works- this is the thing that yanks the user's screen back up.
    }
};

编辑:这打破了它......谢谢@Fabian。

     console.log(FUN + " before ++")
     FUN++;
     console.log(FUN + " after ++")

【问题讨论】:

  • “没有“var”,因此它可以是以下函数的全局变量” 有趣的事实:使用var,它仍然是以下函数的“全局变量”函数(即使不在全局范围内,也不是真正的全局),因为函数会关闭它。更多:How do JavaScript closures work?
  • 您的滚动回调可能触发得太快了,以至于2 的值从未为FUN 注册。你为什么不直接使用if (FUN &gt; 1) 呢?
  • 请使用 Stack Snippets([&lt;&gt;] 工具栏按钮)通过 runnable minimal reproducible example 更新您的问题。 FUN++ 绝对会增加FUN,所以如果你没有看到你想要的效果,问题是别的,而不是它没有被增加。
  • 这个有趣的事实并不有趣...哈哈无论如何,尝试添加一个 else 块和 console.log 的 FUN 变量,也许滚动会很快,你是太快地增加你的变量会导致它没有进入第二个 if。
  • 问题:您显示的代码在页面的哪个位置?什么触发它?在第一次滚动事件之后是否可以重新运行(这会将 FUN 重置为 0)? FWIW,我怀疑关于滚动射击太快的cmets是不正确的;如果是这样,我希望您第一次向下滚动时,您会看到第二个文本出现。我可能是错的;不是说绝对不是,但对我来说似乎不对。

标签: javascript jquery html


【解决方案1】:

实际上,变量 fun 正在增加。您的问题与您使用 jQuery 的 replaceWith 函数有关。您将 h1 替换为文本 id,因此 jQuery 无法在第二次运行时替换它。

这是一个工作演示。 http://codepen.io/StuffieStephie/pen/WpvyjJ

还有一个关于jQuery's replaceWith() and html() 之间区别的好问题

你可以改变

$("#text").replaceWith("<h1>You should not have done that.</h1><br><img src='sck1.jpg'>");

到

$("#text").replaceWith("<h1 id='test'>You should not have done that.</h1><br><img src='sck1.jpg'>");

(注意test 的id)或使用更改您的html 以拥有一个id 为test 的div 并改用jQuery 的.html() 函数。

var FUN = 0;
window.onscroll = function(ev) {
    if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
         console.log(FUN + " before ++");
         FUN++;
         console.log(FUN + " after ++");
         beSpook(FUN);
       $('html, body').animate({ scrollTop: 0 }, 'fast');
    }
};



function beSpook(count){
  if (count ===1){
    console.log(count);
    scream.playclip(); 
    $("#text").html( "<h1>You should not have done that.</h1><br><img src='http://pointlessgame.x10.bz/scroller/sck1.jpg'>" );
  } else if ( count >= 2){
    $("#text").html( "<h1>Last chance to close the page.</h1>" );       
  }
}

    var audiotypes={
        "mp3": "audio/mpeg",
        "mp4": "audio/mp4",
        "ogg": "audio/ogg",
        "wav": "audio/wav"
    }

    function ss_soundbits(sound){
        var audio_element = document.createElement('audio')
        if (audio_element.canPlayType){
            for (var i=0; i<arguments.length; i++){
                var source_element = document.createElement('source')
                source_element.setAttribute('src', arguments[i])
                if (arguments[i].match(/\.(\w+)$/i))
                    source_element.setAttribute('type', audiotypes[RegExp.$1])
                audio_element.appendChild(source_element)
            }
            audio_element.load()
            audio_element.playclip=function(){
                audio_element.pause()
                audio_element.currentTime=0
                audio_element.play()
            }
            return audio_element
        }
    }
    var scream  = ss_soundbits('http://soundbible.com/grab.php?id=1627&type=mp3');
.color_heavy_red{ color: #FF1F1F;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="text">
<h1 class="color_heavy_red" >
Can't get to the bottom of this page.
</h1>
</div>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>

【讨论】:

  • 谢谢斯蒂芬妮!真的帮了大忙。将在包含您的用户名的代码中添加一个复活节彩蛋:)
  • 没问题!我很高兴我能帮上忙 :D 提醒一下:在您的 window.onscroll 函数中,console.log(FUN + ) 行正在破坏东西。要么去掉它,要么把它改成console.log(FUN);不要忘记你的分号和快乐的编码! :)
  • 谢谢。不过,我刚刚意识到,我更新了我的网站代码,但文本直接跳转到第二个文本?
  • 没关系,我找到了!在 sn-p 中有... if (count ===1){。好吧,错误发生了!
  • 啊,太酷了。值得注意的一件事:第二次运行时文本变得非常大,因为它将h1 插入另一个h1。您可能想将您的 html 更改为:&lt;div id="text"&gt; &lt;h1 class="color_heavy_red" &gt; Can't get to the bottom of this page. &lt;/h1&gt; &lt;/div&gt; 但如果您喜欢它,这并不是什么大不了的事;p
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-17
  • 1970-01-01
  • 1970-01-01
  • 2020-05-31
  • 1970-01-01
相关资源
最近更新 更多