javascript - get and set css style into var -


i begginer. trying font size of element variable. after set font size of element using variable.

i have been looking solution 12 hours , found nothing. grateful tips. in advance.

my code below.

jquery(window).scroll(function() {          var oldfont = jquery('.mhmainnav').find('li').find('a').css("font-size");          var windowtop = jquery(window).scrolltop();          if (windowtop > 280) {              jquery('.mh-main-nav').find('li').find('a').css("font-size", "12px");          } else {              jquery('.mh-main-nav').find('li').find('a').css("font-size", oldfont);         }     }); 

the thing change moving old font outside scroll, otherwise when change font-size 12px, oldfont overwritten next time scroll.

maybe cache anchor selector better performance

also check mhmainnav selector - change mh-main-nav

var anchors = jquery('.mhmainnav').find('a'), // if reusing in scroll, cache here better performance   oldfont = anchors.css("font-size"); // set before scroll shouldn't change once set  jquery(window).scroll(function() {   var windowtop = jquery(this).scrolltop();    if (windowtop > 280) {     anchors.css("font-size", "12px");   } else {     anchors.css("font-size", oldfont);   } }); 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -