简体   繁体   English

使用JavaScript而不是jquery打开和关闭菜单

[英]Open and Close a menu with javascript not jquery

I have a Menu that i used CSS to open on a hover but on a mobile that doesn't work so trying to also put a onclick on to it. 我有一个菜单,该菜单是我使用CSS在悬停菜单上打开的,但在无法正常工作的移动设备上打开,因此尝试同时对其进行onclick。 have it open on the onclick but i cant close it. 在onclick上打开它,但我无法关闭它。 the postion i have the menu set at is -200 and then on a hover the position goes left to 0 which makes the menu slide out. 我将菜单设置为-200的位置,然后在悬停位置将位置移至0,这会使菜单滑出。 my javascript is 我的JavaScript是

function openMenu(){
   document.getElementById('colorchanger').style.left = 0 + 'px'        
}

my HTML for that div is 我的那个div的HTML是

<div id="colorchanger" onMouseDown="openMenu();">

I just want the menu to slide back to -200 onclick again. 我只希望菜单再次滑回-200 onclick。

function openMenu(){
  var left=document.getElementById('colorchanger').style.getPropertyValue("left");
  if(left=='-200')
  {
    document.getElementById('colorchanger').style.left = 0 + 'px'
  }
  else
  {
     document.getElementById('colorchanger').style.left = -200 + 'px'
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM