所以基本上我想要实现的是,
我试着玩Jquery,结果仍然没有结果。
答案 0 :(得分:0)
是的,当然。
设置为:
答案 1 :(得分:0)
这(通过一些试验和错误)实际上主要是通过CSS实现的,普通的JavaScript只用于切换类名(当单击一个块时和单击一个关闭链接时)。
如果CSS中的.block.active
被.block:hover
替换,以便每个块都会在悬停时展开,并且删除了关闭链接(和样式),那么根本不需要JS。
<强> HTML:强>
<div class="block">
<div class="closelink">X</div>
</div>
<div class="block">
<div class="closelink">X</div>
</div>
<div class="block">
<div class="closelink">X</div>
</div>
<div class="block">
<div class="closelink">X</div>
</div>
<强> CSS:强>
body {
text-align: center
}
.block {
display: inline-block;
position: relative;
width: 60px;
height: 60px;
background: #08f;
margin: 50px;
cursor: pointer;
border-radius: 8px;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
transition: margin 1s;
-moz-transition: margin 1s;
-webkit-transition: margin 1s;
-o-transition: margin 1s;
-ms-transition: margin 1s;
user-select: none;
-moz-user-select: none;
-webkit-user-select: none
}
.block .closelink {
position: absolute;
width: 20px;
height: 20px;
right: -20px;
top: -20px;
visibility: hidden;
font-size: 14px;
font-family: Segoe UI, Tahoma, Arial, Verdana, sans-serif;
background: #ccc;
border-radius: 4px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px
}
.block.active {
margin-left: 200px;
margin-right: 200px
}
.block.active .closelink {
visibility: visible
}
<强> JavaScript的:强>
window.addEventListener('load', init, false);
function init() {
var blocks = document.getElementsByClassName('block');
var closeLinks = document.getElementsByClassName('closelink');
for(i = 0; i < blocks.length; i++) {
blocks[i].addEventListener('click', blockClicked, false);
}
for(i = 0; i < closeLinks.length; i++) {
closeLinks[i].addEventListener('click', closeLinkClicked, false);
}
}
function blockClicked(e) {
var blocks = document.getElementsByClassName('block');
for(i = 0; i < blocks.length; i++) {
blocks[i].className = 'block';
}
e.target.className = 'block active';
}
function closeLinkClicked(e) {
e.stopPropagation();
e.target.parentElement.className = 'block';
}