简体   繁体   English

你可以帮帮我吗。 我怎样才能使实时变化的价值?

[英]Could you help me. How can I make real-time changing value?

I have a code:我有一个代码:

var appearScroll = function(){
    let fline = document.getElementById("skills__listone");
    var skills = document.getElementById("skills");
    var baselocation = 0;
    fline.style.opacity = 0;
    var distance = window.pageYOffset;
    window.addEventListener("scroll", function(){
        for (i = 0 ; i < 800; i++){
            i = distance;
            console.log(distance);
        }
    })

As you can see, I sent the value of variable to the console log and it should change, when I scroll.如您所见,我将变量的值发送到控制台日志,当我滚动时它应该会改变。 But it doesn't work, and the value changes only when I refresh the page.但它不起作用,只有当我刷新页面时该值才会改变。

Assuming you invoke the appearScroll function somewhere, this function only ever reads the value of window.pageYOffset once .假设你在某处调用appearScroll function,这个 function 只会读取window.pageYOffset的值一次 Then on the scroll event your event handler function logs that one value to the console (up to 800 times for some reason?).然后在scroll事件上,您的事件处理程序 function 将该值记录到控制台(出于某种原因最多 800 次?)。

In order to read a new value in the scroll event, you have to read that value in that event handler.为了在scroll事件中读取新值,您必须在该事件处理程序中读取该值。 For example:例如:

window.addEventListener("scroll", function(){
    console.log(window.pageYOffset);
})

(Logging the same value 800 times also seems unnecessary of course. Though I suppose it's not entirely clear to me what you're trying to do with the various other values involved that aren't being used.) (当然,记录相同的值 800 次似乎也没有必要。虽然我想我并不完全清楚你试图用未使用的其他各种值来做什么。)

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

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