javascript - Using jquery to check if element has class constantly -
i'm using stickyjs create sticky header wordpress website. have enqueued scripts , sticky working perfectly.
what trying achieve check if nav element have set sticky has class called "is-sticky" , if change of css.
here code have already:
function stickyinit($) { $("#site-navigation").sticky({topspacing:0}); window.setinterval(check_sticky,500); function check_sticky(){ if($("#site-navigation-sticky-wrapper").hasclass("is-sticky")){ console.log ("do something"); } } } jquery(document).ready(function ($) { stickyinit($); });
this code in file have enqueued set nav sticky.
as can see have used setinterval try , create loop test "#site-navigation-sticky-wrapper" element see if has class mentioned above.
this code works , message in firebug console, ever once instead of repeatedly.
so question is, how test element see if has class "is-sticky" can alternate nav css when does?
also when class "is-sticky" isnt set on element (after have scrolled top) how set nav original position? or anyway?
check this fiddle - fires repeatedly when 'is-sticky' class active & nav position automatically restored former state when scroll stop.
// code function stickyinit($){ $("#site-navigation").sticky({topspacing:0}); function check_sticky(){ if ( $("#site-navigation-sticky-wrapper").hasclass("is-sticky") ) { console.log ("do something"); } } window.setinterval(check_sticky,500); } jquery(document).ready(function ($) { stickyinit($); });
Comments
Post a Comment