如何添加2个不同的onclick功能到同一个DIV标签

How to add 2 different onclick functions to the same DIV tag

本文关键字:功能 onclick 同一个 标签 DIV 何添加 添加 2个      更新时间:2023-09-26

我刚刚开始设计一个网站,我添加了一个功能,你点击一个标签,它滑动打开一个javascript函数的菜单。但是我不知道如何让div在你第二次点击的时候回到原来的位置,下面是代码

function myFunction() { 
		document.getElementById("toppart1").style.top="-15%";
		document.getElementById("centerbox").style.top="30%";
	}
function backUp() { 
		document.getElementById("toppart2").style.top="0px";
		document.getElementById("centerbox").style.top="23%";
	}
body{
	font-family:"Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
}
#frontpage{
	background-color:navy;
	width:100%;
	height:100%;
	top:25%;
	left:0px;
	position:absolute;
	z-index:2;
}
#toppart1{
	background-color:navy;
	width:100%;
	height:25%;
	top:0px;
	left:0px;
	position:absolute;
	transition:top .5s;
	font-size:small;
	font-weight:bold;
	z-index:1;
}
#toppart1:hover{
	top:-5px;
}
#centerbox{
	background-color:white;
	border:thick black solid;
	opacity: .6;
	width:70%;
	left:15%;
	height:40%;
	top:23%;
	font-family:"Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
	position:absolute;
	transition:top .5s;
}
#menubuttons{
	background-color:white;
	width:auto;
	height:15%;
	top:15%;
	float:left;
	font-weight:bold;
	font-family:"Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
	z-index:-1;
	position:absolute;
	top:10%;
	transition:background-color 1s;
}
#menubuttons:hover{
	background-color:transparent;
	color:navy;
}
#img-arrow{
	background-color:transparent;
	transition:opacity .5s
}
#img-arrow:hover{
		opacity: .5;
	}
<!DOCTYPE html>
<html>
<head>
	<script src="DBWEBSjAVA.js"></script>
	<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
	<link href="Website.css" type="text/css" rel="stylesheet">
<title>Untitled 1</title>
</head>
<body style="background-color:black;">
	<div id="menubuttons">
		<h1 align="center">HOME</h1>
	</div>
		<div id="menubuttons" style="left:15%;">
		<h1 align="center">HOME</h1>
	</div>
		<div id="menubuttons" style="left:29%;">
		<h1 align="center">HOME</h1>
	</div>
		<div id="menubuttons" style="left:43%;">
		<h1 align="center">HOME</h1>
	</div>
		<div id="menubuttons" style="left:57%;">
		<h1 align="center">HOME</h1>
	</div>
	
		<div id="menubuttons" style="left:71%;">
		<h1 align="center">HOME</h1>
	</div>
		<div id="menubuttons" style="left:84%;">
		<h1 align="center">HOME</h1>
	</div>
	<div id="frontpage">
		<div id="centerbox" style="left: 15%; top: 14%; height: 64%" onclick="prettyColors()">
			
		</div>
	</div>
	<div id="toppart1" onclick="myFunction()" onrelease="backUp()" style="left: 0px; top: 0px;">
		<h1 align="center">DB WEBS</h1>
		&nbsp;<img id="img-arrow" src="Untitled.png" style="left:47%; top:67%; position:absolute;" draggable="false" height="36" width="38"/>
		</div>
</body>
</html>

看到它是怎么上升的了吗?我不知道当你第二次点击它的时候怎么让它回到原来的位置。请帮助。

试试这个:

function showOrHide() {
    if (document.getElementById("centerbox").style.top == "30%") {
        backUp();
    } else {
        myFunction();
    }
}
function myFunction() { 
		document.getElementById("toppart1").style.top="-15%";
		document.getElementById("centerbox").style.top="30%";
        document.getElementById("toppart1").style.transform="rotate(180deg)";
	}
function backUp() { 
		document.getElementById("toppart1").style.top="0px";
		document.getElementById("centerbox").style.top="23%";
        document.getElementById("toppart1").style.transform="rotate(0deg)";
	}
body{
	font-family:"Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
}
#frontpage{
	background-color:navy;
	width:100%;
	height:100%;
	top:25%;
	left:0px;
	position:absolute;
	z-index:2;
}
#toppart1{
	background-color:navy;
	width:100%;
	height:25%;
	top:0px;
	left:0px;
	position:absolute;
	transition:top .5s;
	font-size:small;
	font-weight:bold;
	z-index:1;
}
#toppart1:hover{
	top:-5px;
}
#centerbox{
	background-color:white;
	border:thick black solid;
	opacity: .6;
	width:70%;
	left:15%;
	height:40%;
	top:23%;
	font-family:"Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
	position:absolute;
	transition:top .5s;
}
#menubuttons{
	background-color:white;
	width:auto;
	height:15%;
	top:15%;
	float:left;
	font-weight:bold;
	font-family:"Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
	z-index:-1;
	position:absolute;
	top:10%;
	transition:background-color 1s;
}
#menubuttons:hover{
	background-color:transparent;
	color:navy;
}
#img-arrow{
	background-color:transparent;
	transition:opacity .5s
}
#img-arrow:hover{
		opacity: .5;
	}
<!DOCTYPE html>
<html>
<head>
	<script src="DBWEBSjAVA.js"></script>
	<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
	<link href="Website.css" type="text/css" rel="stylesheet">
<title>Untitled 1</title>
</head>
<body style="background-color:black;">
	<div id="menubuttons">
		<h1 align="center">HOME</h1>
	</div>
		<div id="menubuttons" style="left:15%;">
		<h1 align="center">HOME</h1>
	</div>
		<div id="menubuttons" style="left:29%;">
		<h1 align="center">HOME</h1>
	</div>
		<div id="menubuttons" style="left:43%;">
		<h1 align="center">HOME</h1>
	</div>
		<div id="menubuttons" style="left:57%;">
		<h1 align="center">HOME</h1>
	</div>
	
		<div id="menubuttons" style="left:71%;">
		<h1 align="center">HOME</h1>
	</div>
		<div id="menubuttons" style="left:84%;">
		<h1 align="center">HOME</h1>
	</div>
	<div id="frontpage">
		<div id="centerbox" style="left: 15%; top: 14%; height: 64%" onclick="prettyColors()">
			
		</div>
	</div>
	<div id="toppart1" onclick="showOrHide()" style="left: 0px; top: 0px;">
		<h1 align="center">DB WEBS</h1>
		&nbsp;<img id="img-arrow" src="Untitled.png" style="left:47%; top:67%; position:absolute;" draggable="false" height="36" width="38"/>
		</div>
</body>
</html>

最明显的解决方案是使用两个类。然后,您只需要一个函数来在两者之间切换:

function toggle() {
  var topPart = document.getElementById("toppart1");
  var centerbox = document.getElementById("centerbox"); 
  if (!topPart.classList.contains("open")) {
      topPart.classList.add("open");
      centerbox.classList.add("open");
  }
  else {
      topPart.classList.remove("open");
      centerbox.classList.remove("open");
  }
}
body{
	font-family:"Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
}
#frontpage{
	background-color:navy;
	width:100%;
	height:100%;
	top:25%;
	left:0px;
	position:absolute;
	z-index:2;
}
#toppart1{
	background-color:navy;
	width:100%;
	height:25%;
	top:0px;
	left:0px;
	position:absolute;
	transition:top .5s;
	font-size:small;
	font-weight:bold;
	z-index:1;
}
#toppart1:hover{
	top:-5px;
}
#centerbox.open {
   top:30%;
}
  #toppart1.open {
    top:-15%;
  }
   
#centerbox{
	background-color:white;
	border:thick black solid;
	opacity: .6;
	width:70%;
	left:15%;
	height:40%;
	top:23%;
	font-family:"Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
	position:absolute;
	transition:top .5s;
}
#menubuttons{
	background-color:white;
	width:auto;
	height:15%;
	top:15%;
	float:left;
	font-weight:bold;
	font-family:"Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
	z-index:-1;
	position:absolute;
	top:10%;
	transition:background-color 1s;
}
#menubuttons:hover{
	background-color:transparent;
	color:navy;
}
#img-arrow{
	background-color:transparent;
	transition:opacity .5s
}
#img-arrow:hover{
		opacity: .5;
	}
<!DOCTYPE html>
<html>
<head>
	<script src="DBWEBSjAVA.js"></script>
	<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
	<link href="Website.css" type="text/css" rel="stylesheet">
<title>Untitled 1</title>
</head>
<body style="background-color:black;">
	<div id="menubuttons">
		<h1 align="center">HOME</h1>
	</div>
		<div id="menubuttons" style="left:15%;">
		<h1 align="center">HOME</h1>
	</div>
		<div id="menubuttons" style="left:29%;">
		<h1 align="center">HOME</h1>
	</div>
		<div id="menubuttons" style="left:43%;">
		<h1 align="center">HOME</h1>
	</div>
		<div id="menubuttons" style="left:57%;">
		<h1 align="center">HOME</h1>
	</div>
	
		<div id="menubuttons" style="left:71%;">
		<h1 align="center">HOME</h1>
	</div>
		<div id="menubuttons" style="left:84%;">
		<h1 align="center">HOME</h1>
	</div>
	<div id="frontpage">
		<div id="centerbox" style="left: 15%; top: 14%; height: 64%" onclick="prettyColors()">
			
		</div>
	</div>
	<div id="toppart1" onclick="toggle()" style="left: 0px; top: 0px;">
		<h1 align="center">DB WEBS</h1>
		&nbsp;<img id="img-arrow" src="Untitled.png" style="left:47%; top:67%; position:absolute;" draggable="false" height="36" width="38"/>
		</div>
</body>
</html>

你应该给元素一个类来指定它是否被向下滑动,然后在点击时切换这个类。

var el = document.getElementById('toppart1');
if(el.classList.contains("down")){
    el.classList.remove("down");
    document.getElementById('centerbox').classList.remove("down");
}
else {
    el.classList.add("down");
    document.getElementById('centerbox').classList.add("down");
}

然后在CSS中添加:

#toppart1.down {
    top: -15%;
}
#centerbox.down {
    top: 30%;
}