用纯CSS+HTML实现图片点击切换背景

1.实现了点击圆图片,切换对应的背景图片
2.主要技术:css3的背景设置,边框圆角,定位,盒模型,选择器,浮动,锚点,由:target实现切换
一、效果图如下
在这里插入图片描述
二、代码如下:

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title>图片背景切换</title>
		<link rel="shortcut icon" type="image/x-icon" href="img/an.ico"/>
		<style type="text/css">
			/*样式初始化*/
			* {
				margin: 0;
				padding: 0;
				list-style: none;
			}
			/*去除横向滚动条*/
			body {
				overflow-x: hidden;
			}
			/*将div铺满整个窗口*/
			.box {
				height: 100vh;
				min-height: 520px;
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
			}
			/*设置大ul的大小及定位*/
			.content {
				position: relative;
				width: 100%;
				height: 100%;
			}
			/*设置大li样式*/
			.content li {
				position: absolute;
				top: 0;
				left: 0;
				width: 100%;
				height: 100%;
				background-position: center;
				background-clip: border-box;
				background-size: cover;
			}
			/*将第一张图片后的图片隐藏*/
			#li1~li{display: none;}
			/*设置大li背景图片*/
			#li1 {background-image: url(img/bg1.jpg);}
			#li2 {background-image: url(img/bg2.jpg);}
			#li3 {background-image: url(img/bg3.jpg);}
			#li4 {background-image: url(img/bg4.jpg);}
			#li5 {background-image: url(img/bg5.jpg);}
			/*点击a标签,切换大图片*/
			#li1:target,
			#li2:target,
			#li3:target,
			#li4:target,
			#li5:target{
				display: block;
			}
			/*定位*/
			.conlist {
				position: absolute;
				bottom: 0;
			}
			/*设置li的样式*/
			.conlist li {
				position: relative;
				float: left;
				width: 180px;
				height: 150px;
				display: block;
				border: 3px solid #fff;
				border-radius: 10px;
				margin-right: 10px;
				text-align: center;
			}
			/*给li设置背景颜色*/
			.conlist li:first-child {background: #00ff00;}
			.conlist li:nth-child(2) {background: #00ff00;}
			.conlist li:nth-child(3) {background: #66FF00;}
			.conlist li:nth-child(4) {background: #990000;}
			.conlist li:last-child {background: #FF6633;}
			/*设置a标签的样式*/
			.conlist a {
				position: absolute;
				display: block;
				width: 145px;
				height: 145px;
				border: 3px solid #fff;
				border-radius: 75px;
				background-position: center;
				background-clip: border-box;
				background-size: cover;
				left: 10%;
				top: -50%;
			}
			/*设置a标签的背景图片*/
			.a1 {background-image: url(img/sbg1.jpg);}
			.a2 {background-image: url(img/sbg2.jpg);}
			.a3 {background-image: url(img/sbg3.jpg);}
			.a4 {background-image: url(img/sbg4.jpg);}
			.a5 {background-image: url(img/sbg5.jpg);}
			/*设置字体样式*/
			.conlist span {
				line-height: 220px;
				font-family: "黑体";
				font-size: 30px;
				color: #fff;
				font-weight: bold;
			}
		</style>
	</head>

	<body>
		<div class="box">
			<ul class="content">
				<li id="li1"></li>
				<li id="li2"></li>
				<li id="li3"></li>
				<li id="li4"></li>
				<li id="li5"></li>
			</ul>
			<ul class="conlist">
				<li><a href="#li1" class="a1"></a><span>第一张</span></li>
				<li><a href="#li2" class="a2"></a><span>第二张</span></li>
				<li><a href="#li3" class="a3"></a><span>第三张</span></li>
				<li><a href="#li4" class="a4"></a><span>第四张</span></li>
				<li><a href="#li5" class="a5"></a><span>第五张</span></li>
			</ul>
		</div>
	</body>

</html>

需要的同学图片自己更换;

  • 21
    点赞
  • 92
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,为了实现火影忍者的背景切换登录页面,我们需要用到HTMLCSS和JS。首先,我们需要准备好背景图片,这里我选择了几张火影忍者的图片。 接下来,我们可以开始编写代码了。 HTML代码: ```html <!DOCTYPE html> <html> <head> <title>火影忍者登录页面</title> <link rel="stylesheet" type="text/css" href="style.css"> <script type="text/javascript" src="script.js"></script> </head> <body> <div id="bg-img"></div> <div class="container"> <form> <h1>火影忍者登录</h1> <input type="text" placeholder="用户名"> <input type="password" placeholder="密码"> <button type="submit">登录</button> </form> </div> </body> </html> ``` 我们在页面中添加了一个id为“bg-img”的div,用来显示背景图片。我们还添加了一个class为“container”的div,用来包含登录表单。 CSS代码: ```css body { margin: 0; padding: 0; background-color: #000; } #bg-img { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: -1; background-size: cover; background-position: center; animation: bg-slide 20s infinite; } @keyframes bg-slide { 0% { background-image: url("bg1.jpg"); } 25% { background-image: url("bg2.jpg"); } 50% { background-image: url("bg3.jpg"); } 75% { background-image: url("bg4.jpg"); } 100% { background-image: url("bg5.jpg"); } } .container { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: rgba(255, 255, 255, 0.8); padding: 30px; border-radius: 5px; box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.3); } h1 { text-align: center; font-size: 32px; margin-bottom: 20px; } input { display: block; width: 100%; padding: 10px; margin-bottom: 20px; font-size: 16px; border: none; border-radius: 5px; box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.3); } button { display: block; width: 100%; padding: 10px; background-color: #c9302c; color: #fff; font-size: 16px; border: none; border-radius: 5px; cursor: pointer; } button:hover { background-color: #a52725; } ``` 在CSS中,我们设置了背景图片的位置、大小和动画。我们使用了@keyframes来实现背景图片切换,每25%的时间显示一张不同的图片。我们还设置了登录表单的样式,包括位置、颜色、边框和阴影等。 JS代码: ```javascript window.onload = function() { var bgImg = document.getElementById("bg-img"); var bgImgs = ["bg1.jpg", "bg2.jpg", "bg3.jpg", "bg4.jpg", "bg5.jpg"]; var index = 0; setInterval(function() { index++; if (index == bgImgs.length) { index = 0; } bgImg.style.backgroundImage = "url(" + bgImgs[index] + ")"; }, 20000); } ``` 在JS中,我们使用了一个定时器来实现背景图片切换。我们设置了一个数组来存储背景图片的路径,然后每隔20秒就切换一次背景图片。 最后,我们将HTMLCSS和JS保存到同一个文件夹中,然后我们就可以在浏览器中打开这个文件来查看火影忍者的背景切换登录页面了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值