简体   繁体   English

如何在jQuery中编写do while循环

[英]How to write a do while loop in jquery

I'm trying to get a variable to have a set color until a certain scroll distance down the page has been reached, once it reaches that point I want the variable to stop being that color and revert to normal settings. 我试图获取一个具有设定颜色的变量,直到到达页面下方一定的滚动距离为止,一旦到达该点,我希望该变量不再是该颜色并恢复为正常设置。 Once I scroll back up the page, I don't want the color to return. 向上滚动页面后,我不希望颜色返回。 My first thought is the JavaScript do while loop but I'm unsure if that is the best way or even how to do it in jquery. 我的第一个想法是JavaScript的while循环,但是我不确定这是最好的方法,还是如何在jquery中做到这一点。

So far this is what I have 到目前为止,这就是我所拥有的

if($('body').hasClass('page-template-homepage-php')){
    var oneShot = jQuery(window).scrollTop()
    var opacity = jQuery(window).scrollTop() / 100;
     if(opacity > 1){ opacity = 1; }
    //do{
    //  opacity = 1;} while(
    //      oneShot < 201);
    $('.background-wrapper').css('background-color', 'rgba(0, 0, 0, ' + opacity +')');
  }

One of my first attempts was 我的第一个尝试是

for(oneShot < 200; opacity = 1){
    if(oneShot = 200){
        break;
        }
    }

followed by 其次是

do{
    opacity = 1;} while(
        oneShot < 201);

Both evidently with JavaScript instead of jquery 显然都使用JavaScript而不是jquery

Thoughts? 有什么想法吗?

to clarify : Currently, when the page loads, the header in question is a solid black. 需要说明的是 :当前,在页面加载时,相关标题为纯黑色。 Once one pixel has been scrolled, the code posted above makes the header opaque then as the scroll continues down, fades back to black. 一旦滚动了一个像素,上面发布的代码使标题不透明,然后随着滚动继续向下,逐渐变回黑色。 The desired effect is to load page, have header black, scroll down past 100px while header stays black. 所需的效果是加载页面,标题为黑色,向下滚动超过100px,而标题保持黑色。 Upon passing 100px, when scrolling back up the opacity fade takes effect. 传递100像素后,向上滚动时,不透明度褪色将生效。 ** **

A few things before I get to the real question: 在回答真正的问题之前,需要注意以下几点:

  1. As has been said in the comments java is not javascript 正如评论中所说的,java 不是 javascript
  2. And jQuery is a library written in javascript 而jQuery 用JavaScript编写图书馆
  3. In javascript it's never a good idea to write an infinite loop to wait for a certain condition to become true (UI-wise) 在javascript中,编写无限循环以等待特定条件成真(UI方式)绝不是一个好主意

That said, here is a very simple example of how you can approach your problem: 也就是说,这是一个如何解决问题的非常简单的示例:

// attach a listener function to the window.scroll event:
$(window).on('scroll', function scrollListener(evt) {
  // each time the event gets triggered, get the scrollTop-value
  var scrollTopValue = $(window).scrollTop();

  // if it is above/below (don't use equal!) a certain value:
  if (scrollTopValue > 500) {
    // handle that condition:
    $('#test').addClass('foo');

    // remove the listener:
    $(window).off('scroll', scrollListener);
  }
});

demo: http://jsbin.com/sobewute/2/ 演示: http : //jsbin.com/sobewute/2/

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

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