jquery 翻转图片
时间: 2024-01-11 20:04:20 浏览: 78
以下是使用jQuery实现图片翻转效果的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
.flip-container {
perspective: 1000px;
width: 200px;
height: 200px;
position: relative;
}
.flip-container .flipper {
transition: 0.6s;
transform-style: preserve-3d;
position: relative;
transform-origin: center center;
width: 100%;
height: 100%;
}
.flip-container .front, .flip-container .back {
backface-visibility: hidden;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.flip-container .front {
z-index: 2;
transform: rotateY(0deg);
}
.flip-container .back {
transform: rotateY(180deg);
}
.flip-container.flipped .flipper {
transform: rotateY(180deg);
}
</style>
</head>
<body>
<div class="flip-container">
<div class="flipper">
<div class="front">
<img src="front.jpg" alt="Front Image" style="width: 100%; height: 100%;">
</div>
<div class="back">
<img src="back.jpg" alt="Back Image" style="width: 100%; height: 100%;">
</div>
</div>
</div>
<script>
$(document).ready(function() {
$('.flip-container').click(function() {
$(this).toggleClass('flipped');
});
});
</script>
</body>
</html>
```
这段代码使用了CSS的`transform`属性和jQuery的`toggleClass`方法来实现图片的翻转效果。点击图片容器时,会切换容器的`flipped`类,从而改变图片的旋转状态。
阅读全文