简体   繁体   English

希望当Div在视口中时运行JavaScript脚本

[英]Want JavaScript script to run when the Div is in the viewport

I want to run a JS script when a particular div comes into the viewport/is visible. 当特定的div进入视口/可见时,我想运行JS脚本。

The div will always be visible, but when the user scrolls it in to view. div将始终可见,但是当用户滚动它以查看时。

I have created a JSFiddle with the example: 我用示例创建了一个JSFiddle:

Example http://jsfiddle.net/sv8boe9u/21/ 示例http://jsfiddle.net/sv8boe9u/21/

JS JS

consoleText(['HELLO,', 'HERE IS A BIT ABOUT ME,', 'ENJOY!'], 'text', ['#333', '#333', '#333']);

function consoleText(words, id, colors) {

  "use strict";
  if (colors === undefined) {
    colors = ['#fff'];
  }
  var visible = true;
  var con = document.getElementById('console');
  var letterCount = 1;
  var x = 1;
  var waiting = false;
  var target = document.getElementById(id);
  target.setAttribute('style', 'color:' + colors[0]);
  window.setInterval(function() {

    if (letterCount === 0 && waiting === false) {
      waiting = true;
      target.innerHTML = words[0].substring(0, letterCount);
      window.setTimeout(function() {
        var usedColor = colors.shift();
        colors.push(usedColor);
        var usedWord = words.shift();
        words.push(usedWord);
        x = 1;
        target.setAttribute('style', 'color:' + colors[0]);
        letterCount += x;
        waiting = false;
      }, 1000);
    } else if (letterCount === words[0].length + 1 && waiting === false) {
      waiting = true;
      window.setTimeout(function() {
        x = -1;
        letterCount += x;
        waiting = false;
      }, 1000);
    } else if (waiting === false) {
      target.innerHTML = words[0].substring(0, letterCount);
      letterCount += x;
    }
  }, 120);
  window.setInterval(function() {
    if (visible === true) {
      con.className = 'console-underscore hidden';
      visible = false;

    } else {
      con.className = 'console-underscore';

      visible = true;
    }
  }, 400);
}

To clarify, I want it to start at 'Hello' when it is actually in viewport. 为了澄清起见,我希望它实际上在视口中时从“ Hello”开始。 Any ideas? 有任何想法吗?

Thanks. 谢谢。

Using the jQuery scroll() and scrollTop() functions you can specify a height in px that triggers another function when reached such as: 使用jQuery scroll()和scrollTop()函数,您可以指定以px为单位的高度,该高度将在到达时触发另一个函数,例如:

$(window).scroll(function () {
    if ($(this).scrollTop() >= 50) {   // If page is scrolled more than 50px
        doSomething();                // call this function
    } 
});

jQuery Scroll and Scroll Top jQuery 滚动滚动顶部

you can use the scroll function ..like create a function that runs until wen you reach that element u are targeting.. then call the alert('hello'); 您可以使用滚动功能..像创建一个功能,直到您到达要定位的元素为止,然后运行该功能。然后调用alert('hello');

window.addEventListener('scroll', (e) => {

        console.log(window.scrollY)
        if (window.scrollY == 705) {
            alert('got ya')
            // do your stuff here boss
        }
    })

you should make sure to find that scroll position wen yo element comes into view and then put it wr 705 is.. hope this helps ya 您应该确保找到滚动位置wen yo元素,然后将其输入wr 705 is ..希望这对您有所帮助

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

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