六边形,在CSS HTML JavaScript中单击时更改图片
Hexagon with changing pictures on click in CSS HTML JavaScript
我是CSS HTML和JavaScrip的新手。我发现这段代码是为了创建带有图片的六边形图案(请参阅第一个代码)。当我按下六边形时,我希望它的图片变成另一张图片(见第二个代码)。
第一个代码:下面是JavaScript,CSS和HTML。
//I don't know how my java script is going to be
/**
* Generate repeating hexagonal pattern with CSS3 (SO) - 1 element/ hexagon !!!
* http://stackoverflow.com/q/10062887/1397351
*/
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.row {
margin: -18% 0;
text-align: center;
}
.row:first-child {
margin-top: -20%;
}
.hexagon {
position: relative;
display: inline-block;
overflow: hidden;
margin: 0 8.5%;
padding: 16%;
transform: rotate(30deg) skewY(30deg) scaleX(.866);
/* .866 = sqrt(3)/2 */
}
.hexagon:before,
.content:after {
display: block;
position: absolute;
/* 86.6% = (sqrt(3)/2)*100% = .866*100% */
top: 6.7%;
right: 0;
bottom: 6.7%;
left: 0;
/* 6.7% = (100% -86.6%)/2 */
transform: scaleX(1.155) skewY(-30deg) rotate(-30deg);
/* 1.155 = 2/sqrt(3) */
background-color: rgba(30, 144, 255, .56);
background-size: cover;
content: '';
}
t:after {
content: attr(data-content);
}
.row:first-child .hexagon:first-child:before {
background-image: url(http://s10.postimg.org/o0vmcz3hl/Untitled_2_0.jpg);
}
.row:nth-child(2) .hexagon:nth-child(2):before {
background-image: url(http://s10.postimg.org/n7j0kcxgp/Untitled_2_0.jpg);
}
.row:nth-child(3) .hexagon:first-child:before {
background-image: url(http://s10.postimg.org/pen98a2qx/Untitled_2_0.jpg);
}
.row:nth-child(4) .hexagon:nth-child(2):before {
background-image: url(http://s10.postimg.org/ivuevcqjt/Untitled_2_0.jpg);
}
.row:nth-child(2) .hexagon:first-child:before {
background-image: url(http://s10.postimg.org/dvwynekx5/Untitled_2_0.jpg);
}
.row:nth-child(4) .hexagon:first-child:before {
background-image: url(http://s10.postimg.org/7ak8nn52h/Untitled_2_0.jpg);
}
.row:nth-child(5) .hexagon:first-child:before {
background-image: url(http://s10.postimg.org/i7lkceru1/Untitled_2_0.jpg);
}
<!-- content to be placed inside <body>…</body> -->
<div class='row'>
<div class='hexagon'></div>
</div>
<div class='row'>
<div class='hexagon'></div>
<div class='hexagon'></div>
</div>
<div class='row'>
<div class='hexagon'></div>
</div>
<div class='row'>
<div class='hexagon'></div>
<div class='hexagon'></div>
</div>
<div class='row'>
<div class='hexagon'></div>
</div>
第二个代码:下面是JavaScript,CSS和HTML。
var bodyObj, className, index;
bodyObj = document.getElementById('body');
index = 1;
className = [
'imageOne',
'imageTwo'
];
function updateIndex() {
if (index === 0) {
index = 1;
} else {
index = 0;
}
}
bodyObj.onclick = function(e) {
e.currentTarget.className = className[index];
updateIndex();
}
html,
body,
#body {
height: 100%;
width: 100%;
}
#body.imageOne {
background-image: url("http://s10.postimg.org/7ak8nn52h/Untitled_2_0.jpg");
}
#body.imageTwo {
background-image: url("http://s27.postimg.org/qqz5ww52r/red_4.jpg");
}
<div id="body" class="imageOne"></div>
结果: jsfiddle
我添加了一个新的类.hexagon-hide(与.hexagon相同,但背景图像不同),然后我添加了JQuery函数,只需单击hexagondiv即可在.hexagon和.hexagon-hide之间切换。
$( ".hexagon" ).click(function() {
$( this ).toggleClass('hexagon hexagon-hide');
});
/**
* Generate repeating hexagonal pattern with CSS3 (SO) - 1 element/ hexagon !!!
* http://stackoverflow.com/q/10062887/1397351
*/
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.row {
margin: -18% 0;
text-align: center;
}
.row:first-child {
margin-top: -20%;
}
.hexagon, .hexagon-hide {
position: relative;
display: inline-block;
overflow: hidden;
margin: 0 8.5%;
padding: 16%;
transform: rotate(30deg) skewY(30deg) scaleX(.866);
/* .866 = sqrt(3)/2 */
}
.hexagon:before, .hexagon-hide:before,
.content:after {
display: block;
position: absolute;
/* 86.6% = (sqrt(3)/2)*100% = .866*100% */
top: 6.7%;
right: 0;
bottom: 6.7%;
left: 0;
/* 6.7% = (100% -86.6%)/2 */
transform: scaleX(1.155) skewY(-30deg) rotate(-30deg);
/* 1.155 = 2/sqrt(3) */
background-color: rgba(30, 144, 255, .56);
background-size: cover;
content: '';
}
t:after {
content: attr(data-content);
}
.row:first-child .hexagon:first-child:before {
background-image: url(http://s10.postimg.org/o0vmcz3hl/Untitled_2_0.jpg);
}
.row:nth-child(2) .hexagon:nth-child(2):before {
background-image: url(http://s10.postimg.org/n7j0kcxgp/Untitled_2_0.jpg);
}
.row:nth-child(3) .hexagon:first-child:before {
background-image: url(http://s10.postimg.org/pen98a2qx/Untitled_2_0.jpg);
}
.row:nth-child(4) .hexagon:nth-child(2):before {
background-image: url(http://s10.postimg.org/ivuevcqjt/Untitled_2_0.jpg);
}
.row:nth-child(2) .hexagon:first-child:before {
background-image: url(http://s10.postimg.org/dvwynekx5/Untitled_2_0.jpg);
}
.row:nth-child(4) .hexagon:first-child:before {
background-image: url(http://s10.postimg.org/7ak8nn52h/Untitled_2_0.jpg);
}
.row:nth-child(5) .hexagon:first-child:before {
background-image: url(http://s10.postimg.org/i7lkceru1/Untitled_2_0.jpg);
}
.row:first-child .hexagon-hide:first-child:before {
background-image: url("http://s27.postimg.org/qqz5ww52r/red_4.jpg");}
.row:nth-child(2) .hexagon-hide:nth-child(2):before {
background-image: url("http://s27.postimg.org/qqz5ww52r/red_4.jpg");}
.row:nth-child(3) .hexagon-hide:first-child:before {
background-image: url("http://s27.postimg.org/qqz5ww52r/red_4.jpg");}
.row:nth-child(4) .hexagon-hide:nth-child(2):before {
background-image: url("http://s27.postimg.org/qqz5ww52r/red_4.jpg");}
.row:nth-child(2) .hexagon-hide:first-child:before {
background-image: url("http://s27.postimg.org/qqz5ww52r/red_4.jpg");}
.row:nth-child(4) .hexagon-hide:first-child:before {
background-image: url("http://s27.postimg.org/qqz5ww52r/red_4.jpg");}
.row:nth-child(5) .hexagon-hide:first-child:before {
background-image: url("http://s27.postimg.org/qqz5ww52r/red_4.jpg");}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<!-- content to be placed inside <body>…</body> -->
<div class='row'>
<div class='hexagon'></div>
</div>
<div class='row'>
<div class='hexagon'></div>
<div class='hexagon'></div>
</div>
<div class='row'>
<div class='hexagon'></div>
</div>
<div class='row'>
<div class='hexagon'></div>
<div class='hexagon'></div>
</div>
<div class='row'>
<div class='hexagon'></div>
</div>
相关文章:
- 单击更改图标并通过javascript添加一个css类
- 如果30秒未单击,请应用CSS一次,将其删除,然后重新迭代
- 单击时如何更改单选按钮的CSS
- 使用javascript单击同一按钮,在两种样式之间更改css值
- 单击css动画后丢失的事件
- 在css/javascript中单击,切换图像上的去饱和度
- casperjs-css选择器存在,但当单击它时会引发“”;CasperError:无法在不存在的选择器“”上调度mou
- 单击启动CSS导航活动类更改
- 阻止在单击和关闭覆盖菜单css时滚动正文
- 在可视化中将CSS从悬停更改为单击
- 单击禁用的按钮添加 CSS
- 单击相应的“btn”时编辑“项目”的css
- 如何在单击时更改序号元素的CSS
- 单击标签后,使用Javascript更改正文CSS
- 在angularJs中单击更改css类
- 单击后使CSS悬停元素永久化
- 需要在bootstrap css中单击时将图像加载为模态
- 单击CSS备选方案
- 在单击CSS上创建下拉菜单
- 单击CSS框加载另一个网页.CSS 或 js