我想只用CSS缩放图像.下面的代码在按住鼠标左键的同时缩放图像,但我想用鼠标点击放大和缩小.我怎样才能做到这一点?
.container img {
transition: transform 0.25s ease;
cursor: zoom-in;
}
.container img:active {
-webkit-transform: scale(2);
transform: scale(2);
cursor: zoom-out;
}
Run Code Online (Sandbox Code Playgroud)
Nha*_*han 20
我们在这里使用一个技巧,一个输入复选框:
input[type=checkbox] {
display: none;
}
.container img {
margin: 100px;
transition: transform 0.25s ease;
cursor: zoom-in;
}
input[type=checkbox]:checked ~ label > img {
transform: scale(2);
cursor: zoom-out;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
<input type="checkbox" id="zoomCheck">
<label for="zoomCheck">
<img src="https://placehold.it/200">
</label>
</div>
Run Code Online (Sandbox Code Playgroud)
Law*_*one 12
基于@Nhan 的回答:https ://stackoverflow.com/a/39859268/661872
更短、有范围且不需要跟踪多个元素的 ID。
.click-zoom input[type=checkbox] {
display: none
}
.click-zoom img {
margin: 100px;
transition: transform 0.25s ease;
cursor: zoom-in
}
.click-zoom input[type=checkbox]:checked~img {
transform: scale(2);
cursor: zoom-out
}
Run Code Online (Sandbox Code Playgroud)
<div class="click-zoom">
<label>
<input type="checkbox">
<img src="https://via.placeholder.com/200">
</label>
</div>
<div class="click-zoom">
<label>
<input type="checkbox">
<img src="https://via.placeholder.com/200">
</label>
</div>
Run Code Online (Sandbox Code Playgroud)