How can I make the pixel value of the height in my code dynamic?

therealqwerpy

I've built a small script to let chosen objects fade in when the user scrolls down. My problem is that this script is pretty static. If I applied this on 20 different objects, say, I would have to set the height every time. Here is an example:

$(document).ready(function () {
  $(window).scroll(function () {
    if ($(this).scrollTop() > 500) {
      $(".header-js-scroll-fade").css({"opacity" : "1"});
      $(".home-vorteile").addClass("header-img-fade-in-by-scroll");
    }
    else {
      $(".header-js-scroll-fade").css({"opacity" : "0"});
      $(".home-vorteile").removeClass("header-img-fade-in-by-scroll");
    }
  });
});
.header-js-scroll-fade {
	opacity: 0;
	transition: 0.5s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<h1 id="vorteile-text">Vertrauen durch,</h1>

<section class="home-vorteile">
  <div class="header-js-scroll-fade header-img-fade-in-by-scroll">
    <img src="https://via.placeholder.com/500" />
    <h2>Sicherheit.</h2>
  </div>
  <div class="header-js-scroll-fade header-img-fade-in-by-scroll">
    <img src="https://via.placeholder.com/500" />
    <h2>Neueste KI Technik.</h2>
  </div>
  <div class="header-js-scroll-fade header-img-fade-in-by-scroll">
    <img src="https://via.placeholder.com/500" />
    <h2>Beste Materialien.</h2>
  </div>
</section>

NoNickAvailable

This checks if the element is visible by the user, if so it adds the class, if not it removes it. To make this happend you just need to apply the class header-js-scroll-fade to any element you want.

The function isInViewport is from the User Tom Pažourek, found here: https://stackoverflow.com/a/40658647/8605830

// https://stackoverflow.com/a/40658647/8605830
$.fn.isInViewport = function() {
    var elementTop = $(this).offset().top;
    var elementBottom = elementTop + $(this).outerHeight();

    var viewportTop = $(window).scrollTop();
    var viewportBottom = viewportTop + $(window).height();

    return elementBottom > viewportTop && elementTop < viewportBottom;
};

$(document).ready(function () {

  $(window).scroll(function () {
  
    $('.header-js-scroll-fade').each(function () {
      if ($(this).isInViewport()) {
        $(this).addClass("visible");
      }
      else {
        $(this).removeClass("visible");
      }
      
    });
    
  });
  
});
.header-js-scroll-fade {
	opacity: 0;
	transition: 0.5s;
}

.header-js-scroll-fade.visible {
	opacity: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<h1 id="vorteile-text">Vertrauen durch,</h1>

<section class="home-vorteile">
  <div class="header-js-scroll-fade">
    <img src="https://via.placeholder.com/500" />
    <h2>Sicherheit.</h2>
  </div>
  <div class="header-js-scroll-fade">
    <img src="https://via.placeholder.com/500" />
    <h2>Neueste KI Technik.</h2>
  </div>
  <div class="header-js-scroll-fade">
    <img src="https://via.placeholder.com/500" />
    <h2>Beste Materialien.</h2>
  </div>
</section>

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Kivy-how can i make my canvas height smaller than the parent height

分類Dev

How can i make a dynamic form in vuejs

分類Dev

how do i make my code shorter

分類Dev

How can we make the height of second div same as the height of first div which is dynamic?

分類Dev

How do I make my column dynamic in HTML

分類Dev

How can I make this code if and else?

分類Dev

How can I correct my Java code ?

分類Dev

How can I correct my Java code ?

分類Dev

How can I make a pseudo-dynamic Stored procedure / CTE

分類Dev

How can I implement a dynamic class in my view?

分類Dev

Why does my prime factorization code in processing 3 use so much memory and how can I make it use less memory?

分類Dev

How can I make a button redirect my page to another page using a controller action and passing a value using javascript?

分類Dev

How can i make my database method more future proof?

分類Dev

How can I make my glob work for cogs on Linux

分類Dev

How can I make my trie more efficient?

分類Dev

How can I make a table inside my shiny box with RandomIcon()

分類Dev

How can I make a triangle drawable on the top left of my view?

分類Dev

How can I make my button call a rotation of GIFs?

分類Dev

How can I make my sprite not go crosswise in pygame?

分類Dev

How can I make my div's appear in a horizontal way?

分類Dev

How can I make my bot delete its own message?

分類Dev

My Prestashop is slow - how can I make it faster?

分類Dev

How can I make this code thread-safe

分類Dev

How can I make a Python string to include unicode code points?

分類Dev

How can I make a code more efficient and shorter?

分類Dev

Can i make a MySQL dynamic insert on php?

分類Dev

How can I work out the errors in my automation code?

分類Dev

How can i simplify my current React code?

分類Dev

How can I shorten my code with for() or something else in JavaScript?

Related 関連記事

  1. 1

    Kivy-how can i make my canvas height smaller than the parent height

  2. 2

    How can i make a dynamic form in vuejs

  3. 3

    how do i make my code shorter

  4. 4

    How can we make the height of second div same as the height of first div which is dynamic?

  5. 5

    How do I make my column dynamic in HTML

  6. 6

    How can I make this code if and else?

  7. 7

    How can I correct my Java code ?

  8. 8

    How can I correct my Java code ?

  9. 9

    How can I make a pseudo-dynamic Stored procedure / CTE

  10. 10

    How can I implement a dynamic class in my view?

  11. 11

    Why does my prime factorization code in processing 3 use so much memory and how can I make it use less memory?

  12. 12

    How can I make a button redirect my page to another page using a controller action and passing a value using javascript?

  13. 13

    How can i make my database method more future proof?

  14. 14

    How can I make my glob work for cogs on Linux

  15. 15

    How can I make my trie more efficient?

  16. 16

    How can I make a table inside my shiny box with RandomIcon()

  17. 17

    How can I make a triangle drawable on the top left of my view?

  18. 18

    How can I make my button call a rotation of GIFs?

  19. 19

    How can I make my sprite not go crosswise in pygame?

  20. 20

    How can I make my div's appear in a horizontal way?

  21. 21

    How can I make my bot delete its own message?

  22. 22

    My Prestashop is slow - how can I make it faster?

  23. 23

    How can I make this code thread-safe

  24. 24

    How can I make a Python string to include unicode code points?

  25. 25

    How can I make a code more efficient and shorter?

  26. 26

    Can i make a MySQL dynamic insert on php?

  27. 27

    How can I work out the errors in my automation code?

  28. 28

    How can i simplify my current React code?

  29. 29

    How can I shorten my code with for() or something else in JavaScript?

ホットタグ

アーカイブ