Posted in jquery
19
12:24 am, December 1, 2020

using jquery appear for checking if an element is visible on screen

Yes.. sometimes i like using jquery as well, as it can make things so easy! :)

Here is a quick demo using the JQuery Appear plugin so make an element change if it on or off screen. 

Plugin Link: https://cdnjs.com/libraries/jquery.appear 

This requires JQuery and also a Document Ready

* Still trying to get this demo code working properly

HTML

<div class="moo" id="moo">I Am Hidden (almost)</div>

CSS

.moo {
 height:100px;
 width:300px;
 border:3px solid #333;
 padding:30px;
 border-radius:3px;
 text-align:center;
}

Scripts

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.appear/0.4.1/jquery.appear.js" integrity="sha512-dPZE9SLz1NVUX9tSX9TYOs2SyaJRBZC9ogOc/VasDFSFrxSDkXv3ZCdgkG8/AzHQ+YVklyqDgSS3aDgMradlNg==" crossorigin="anonymous"></script>

Javascript

$(document).ready(function(){
  $('#moo').on('appear', function(event, $all_appeared_elements) {
    // this element is now inside browser viewport
    $("#moo").text("i am visible");
  });
  $('someselector').on('disappear', function(event, $all_disappeared_elements) {
    // this element is now outside browser viewport
    $("#moo").text("i am <b>NOT</b> visible");
  });
});

/*
$(document).ready(function(){
 $('#moo').appear(function() {
  $("#moo").text("moo");
 });
});
$('<div>test</div>').appear();
*/
I Am Hidden (almost)

View Statistics
This Week
141
This Month
317
This Year
0

No Items Found.

Add Comment
Type in a Nick Name here
 
Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

Join me on Substack if you want me to send you a collection of the things i have done or found or read for the week. Or follow me on twitter if you prefer, i dont post much but i probably should!

❤👩‍💻🕹

Random Quote
Ever tried. Ever failed. No matter. Try again. Fail again. Fail better. You won't believe what you can accomplish by attempting the impossible with the courage to repeatedly fail better.
Unknown