1.代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js">
</script>
<style>
img {
width: 200px;
height: 200px;
transition: filter 0.5s;
}
</style>
</head>
<body>
<img id="color-change" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F201911%2F13%2F20191113142646_zwliy.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1723865165&t=08248e78361a7286d9886b68620247ca" alt="Original Image">
<script>
$(document).ready(function(){
$("#color-change").click(function(){
$(this).attr("src", "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201810%2F06%2F20181006174836_PGHmM.jpeg&refer=http%3A%2F%2Fb-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1723865534&t=f53d7807a484a98bd174a33ac9eb6908g");
});
});
</script>
</body>
</html>
点前:
点后:
2.代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>背景渐变方向变换</title>
<style>
body {
background-image: linear-gradient(to right, red, yellow); /* 默认垂直渐变 */
background-size: 200% 200%;
animation: gradient 5s ease infinite;
}
@keyframes gradient {
0% {
background-position: 0% 50%;
}
50% {
background-position: 100% 50%;
}
100% {
background-position: 0% 50%;
}
}
</style>
</head>
<body>
<script>
document.addEventListener('click', function() {
var currentPosition = window.getComputedStyle(document.body).backgroundPosition;
var currentPositionX = currentPosition.split(' ')[0];
var currentPositionY = currentPosition.split(' ')[1];
if (currentPositionX === '0%') {
document.body.style.backgroundPosition = '100% 50%'; // 切换到水平渐变
} else {
document.body.style.backgroundPosition = '0% 50%'; // 切换回垂直渐变
}
});
</script>
</body>
</html>
点前:
点后: