javascript - Unexpected behavior with class added by jQuery -
a tale of 2 fiddles (please use run button after jsfiddle pages load clearer idea of happeneing).
dead simple:
$("body").addclass("noscroll"); alert($("body").hasclass("noscroll")); $("body").removeclass("noscroll"); alert($("body").hasclass("noscroll"));
with css:
.noscroll { background-color: pink; position: fixed; width: 100%; top: 200px; }
we have class. class added body, changing body's appearance/behavior. class removed body , body reverts default. working expected.
$("body").addclass("noscroll"); alert($("body").hasclass("noscroll")); $(".noscroll").css({ "background-color" : "pink", "position" : "fixed", "width" : "100%", "top" : "200px" }); $("body").removeclass("noscroll"); alert($("body").hasclass("noscroll"));
no accompanying css time, it's added jquery, otherwise pretty similar above. working point. css applied, isn't removed. why happening?
thanks!
for second fiddle, when call css() on noscroll
selector, applies styles inline element class noscroll
. however, styles not preserved in named css style.
so code working. adding class noscroll
, no styles affiliated class in css. also, removing class, styles css()
call stay because applied inline.
to better idea, see fiddle inline style removed manually @ end.
Comments
Post a Comment