1.使用Jquery完成点击图片变换图片颜色
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="C:\code\JQuery.js"></script>
<title>点击图片变换颜色</title>
<style>
#box {
width: 300px;
height: 300px;
background-color: red;
}
</style>
</head>
<body>
<div id="box"></div>
<script>
$(document).ready(function () {
$("#box").click(function () {
$(this).css("background-color", "blue");
});
});
</script>
</body>
</html>
。
2.使用JS中的DOM操作完成背景颜色渐变方向变换。点击背景,渐变方向发生改变。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>背景颜色渐变方向变换</title>
<style>
body {
height: 100vh;
margin: 0;
transition: background 0.5s;
}
</style>
</head>
<body>
<script>
let direction = 'to right';
document.body.onclick = () => {
document.body.style.background = `linear-gradient(${direction}, #ff6666, #0099cc)`;
direction = direction === 'to right' ? 'to bottom' : 'to right';
};
// 初始化背景
document.body.click();
</script>
</body>
</html>