how to convert jquery hide/show+animation to javascript? -
i want create left side menu.
i want open , close button.
currently use jquery code:
<script> $("#openmenu").click(function() { var menu = $("#menu"); if ($(menu).is(":visible")) { $(menu).animate({width: 0}, 1000, function() {$(menu).hide();}); } else { $(menu).show().animate({width: 200}, 1000); } }); </script>
how can pure javascript?
as gomzy says can use css display property hide menu.
however if want animate need this:
(function () { var menu = { //node menu container wish manipulate node : document.getelementbyid("menu"), //is menu open? open : false, //a handle our interval interval : null, //how wide open menu? openwidth : 200, //how many pixels should open/close per frame? (speed of transition determined here) pixelperframe : 10, //load elements has "togglemenu" class , bind "onclick" event them init : function () { //self refer dont confused when handling events var self = this; //fetching elements var togglers = document.getelementsbyclassname("togglemenu"); //looping through elements (var of togglers) { //bind event a.onclick = function (e) { //here need "self" variable self.toggle(); } } }, //open/close menu toggle : function () { //self refer dont confused when handling events var self = this; //if menu doesn't yet have width modifier, assume menu open if (self.node.style.width == "") { self.node.style.width = self.openwidth + "px"; self.open = true; } //revert menu direction. closes when menu open , opens if menu closed self.open = !self.open; //clear previous menus clearinterval(self.interval); //setup interval function self.interval = setinterval(function () { //if menu opening if (self.open) { //add "pixelperframe" width current width of menu self.node.style.width = parseint(parseint(self.node.style.width.split("px")) + self.pixelperframe) + "px"; //if menu wide or wider "openwidth", set width "openwidth" , clears interval if (parseint(self.node.style.width.split("px")) >= self.openwidth) { self.node.style.width = self.openwith + "px"; clearinterval(self.interval); } //if menu closing } else { //subtracts "pixelperframe" width current width of menu self.node.style.width = parseint(parseint(self.node.style.width.split("px")) - self.pixelperframe) + "px"; //if menu smaller or equal 0 pixels, set width "0px" , clears interval if (parseint(self.node.style.width.split("px")) <= 0) { self.node.style.width = "0px"; clearinterval(self.interval); } } }, 1000 / 60); } } //runs init menu.init(); })();
https://jsfiddle.net/g23zr90b/
are there specific reason why don't want use jquery?
Comments
Post a Comment