JQuery有很多這樣的插件,但是我們公司不用jquery,沒有插件,所以我就試著自己寫,我也不知道別人是怎么寫的,純粹是按著自己的想法來的。
直接上代碼:
復制代碼 代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
<script type="text/javascript">
window.onload = function(){
var btn = document.getElementsByTagName('button')[0];
var flt = document.getElementsByTagName('div')[0];
btn.onclick = function(){
event.cancelBubble = true;
var x = btn.offsetLeft - 15 + 'px';
var y = btn.offsetTop - 100 + 'px';
flt.style.top = y;
flt.style.left = x;
flt.style.display = 'block';
}
document.onclick = function(){
flt.style.display = 'none';
}
}
</script>
<style type="text/css">
*{
margin: 0px;
padding: 0px;
}
div{
width: 60px;
height: 100px;
background: #33ccff;
display: none;
position: absolute;
}
div ul{
text-align: center;
}
div li{
list-style-type: none;
}
button{
top: 300px;
left: 400px;
position: absolute;
}
</style>
</head>
<body>
<button>Click</button>
<div>
<ul>
<li><a href="">Demo 1</a></li>
<li><a href="">Demo 2</a></li>
<li><a href="">Demo 3</a></li>
<li><a href="">Demo 4</a></li>
<li><a href="">Demo 5</a></li>
</ul>
</div>
</body>
</html>
新聞熱點
疑難解答
圖片精選