Событие при скролле

257
11 апреля 2018, 07:23

$(window).scroll(function() { 
 var features = $('.features').offset().top; 
 if($(this).scrollTop() > features) { 
   $('.features').css('background-color','red'); 
 } 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<div class="top-menu"> 
  <div class="logo"><a href="#"><span>New</span>Providence</a></div> 
    <div class="burger-menu"> 
      <div class="burger-menu-top"></div> 
      <div class="burger-menu-middle"></div> 
      <div class="burger-menu-middle"></div> 
    </div> 
    <div class="menu1 menu-hidden menu-visually-hidden"> 
      <span><a href="">Tour</a></span> 
      <span><a href="#">Features</a></span> 
      <span><a href="#">Pricing</a></span> 
    </div> 
 
    <div class="menu2 menu-hidden menu-visually-hidden"> 
      <span  class="help"><a href="#">Help</a></span> 
      <span><a  href="#">Contacts</a></span> 
      <span><button class=""><a href="#">Get  App</a></button></span> 
    </div> 
  </div> 
     <div class="top-text"> 
      <h2>What happens tomorrow?</h2> 
        <p>The sight of the tumblers restored Bob Sawyer to a degree of equanimity which he had not possessed since his interview with his landlady. His face brightened up, and he began to feel quite convivial.</p> 
          <button class="flat-buttons watch-video">Watch video</button> 
         </div> 
        <div class="Iphone-6"> 
      </div> 
    </div> 
  </div> 
  <div class="layout"> 
    <div class="features"> 
      <div class="feature"> 
        <div class="feature-icon1"></div> 
        <h2>Real-time all the time</h2> 
        <p>Thus much I thought proper to tell you in relation to yourself, 
        Thus much I thought proper to tell you in relation to yourself, and to the trust I reposed in you.</p> 
        <p>Just then her head struck against the roof of the hall: in fact she was now more than nine feet high, and she at once took up the little golden key and hurried off to the garden door. 
        </p> 
      </div> 
      <div class="feature"> 
        <div class="feature-icon2"></div> 
        <h2>Adopt without any obstacles</h2> 
        <p>This sounded a very good reason, and Alice was quite pleased to</p> 
      <p>Just then her head struck against the roof of the hall: in fact she was  
        now more than nine feet high, and she at once took up the little golden key and hurried off to the garden door. 
      </p> 
    </div> 
  </div> 
</div>

Сделал таким образом но есть некоторые ошибки. 1) Событие срабатывает на половине блока, а должен как только блок попадает в экран 2) При обновлении эфект не срабатывает пока не начнешь листать вниз или вверх Как можно реализовать данные функции???

Answer 1

$(document).ready(function() { 
  var features = $('.features').offset().top; 
  if (features == 450) { 
    $('.features').css('background-color', '#red'); 
  } else { 
    $('.features').css('background-color', 'transparent'); 
  } 
}); 
$(window).scroll(function() { 
  var features = $('.features').offset().top; 
  if ($(this).scrollTop() > features - 450) { 
    $('.features').css('background-color', 'red'); 
  } else { 
    $('.features').css('background-color', 'transparent'); 
  } 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<div class="top-menu"> 
  <div class="logo"><a href="#"><span>New</span>Providence</a></div> 
  <div class="burger-menu"> 
    <div class="burger-menu-top"></div> 
    <div class="burger-menu-middle"></div> 
    <div class="burger-menu-middle"></div> 
  </div> 
  <div class="menu1 menu-hidden menu-visually-hidden"> 
    <span><a href="">Tour</a></span> 
    <span><a href="#">Features</a></span> 
    <span><a href="#">Pricing</a></span> 
  </div> 
 
  <div class="menu2 menu-hidden menu-visually-hidden"> 
    <span class="help"><a href="#">Help</a></span> 
    <span><a  href="#">Contacts</a></span> 
    <span><button class=""><a href="#">Get  App</a></button></span> 
  </div> 
</div> 
 
 
<div class="top-text"> 
  <h2>What happens tomorrow?</h2> 
  <p>The sight of the tumblers restored Bob Sawyer to a degree of equanimity which he had not possessed since his interview with his landlady. His face brightened up, and he began to feel quite convivial.</p> 
  <button class="flat-buttons watch-video">Watch video</button> 
 
</div> 
<div class="Iphone-6"> 
</div> 
<div class="top-text"> 
  <h2>What happens tomorrow?</h2> 
  <p>The sight of the tumblers restored Bob Sawyer to a degree of equanimity which he had not possessed since his interview with his landlady. His face brightened up, and he began to feel quite convivial.</p> 
  <button class="flat-buttons watch-video">Watch video</button> 
 
</div> 
<div class="Iphone-6"> 
</div> 
<div class="top-text"> 
  <h2>What happens tomorrow?</h2> 
  <p>The sight of the tumblers restored Bob Sawyer to a degree of equanimity which he had not possessed since his interview with his landlady. His face brightened up, and he began to feel quite convivial.</p> 
  <button class="flat-buttons watch-video">Watch video</button> 
 
</div> 
<div class="Iphone-6"> 
</div> 
</div> 
</div> 
<div class="layout"> 
  <div class="features"> 
    <div class="feature"> 
      <div class="feature-icon1"></div> 
      <h2>Real-time all the time</h2> 
      <p>Thus much I thought proper to tell you in relation to yourself, Thus much I thought proper to tell you in relation to yourself, and to the trust I reposed in you.</p> 
      <p>Just then her head struck against the roof of the hall: in fact she was now more than nine feet high, and she at once took up the little golden key and hurried off to the garden door. 
      </p> 
    </div> 
    <div class="feature"> 
      <div class="feature-icon2"></div> 
      <h2>Adopt without any obstacles</h2> 
      <p>This sounded a very good reason, and Alice was quite pleased to</p> 
      <p>Just then her head struck against the roof of the hall: in fact she was now more than nine feet high, and she at once took up the little golden key and hurried off to the garden door. 
      </p> 
    </div> 
  </div> 
  <div class="top-text"> 
    <h2>What happens tomorrow?</h2> 
    <p>The sight of the tumblers restored Bob Sawyer to a degree of equanimity which he had not possessed since his interview with his landlady. His face brightened up, and he began to feel quite convivial.</p> 
    <button class="flat-buttons watch-video">Watch video</button> 
    <div class="top-text"> 
      <h2>What happens tomorrow?</h2> 
      <p>The sight of the tumblers restored Bob Sawyer to a degree of equanimity which he had not possessed since his interview with his landlady. His face brightened up, and he began to feel quite convivial.</p> 
      <button class="flat-buttons watch-video">Watch video</button> 
      <div class="top-text"> 
        <h2>What happens tomorrow?</h2> 
        <p>The sight of the tumblers restored Bob Sawyer to a degree of equanimity which he had not possessed since his interview with his landlady. His face brightened up, and he began to feel quite convivial.</p> 
        <button class="flat-buttons watch-video">Watch video</button> 
 
      </div>

READ ALSO
jquery через on не видит данные загруженные через ajax

jquery через on не видит данные загруженные через ajax

Добрый день, загружаю форму через ajax, навешиваю к примеру на input id='test'

165
Яндекс Карты и подгруженный контент по аяксу

Яндекс Карты и подгруженный контент по аяксу

На сайте есть контент, которые обновляется по фильтрам (выбрал чекбокс - перезагрузка контента по аяксу)В контенте есть ссылки на попап-карты

224
Как подключить jQuery Migrate к webpack?

Как подключить jQuery Migrate к webpack?

Я подключаю jQuery следующим образом:

182
Универсальный метод скриншота окна

Универсальный метод скриншота окна

Нашел здесь отличное решения для создания скриншотов по дескриптору окнаЯ его немного подредактировал под свои нужды, получилось следующее:

184