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
Post a Comment