纯CSS实现图片切换(按键切换/自动切换)

本篇文章主要是实现通过纯CSS实现图片切换和定时切换

一、通过点击切换

效果图如下

html

<div id="hewenqislider" class="csslider">
    <input type="radio" name="slides" id="slides_1" checked />
    <input type="radio" name="slides" id="slides_2" />
    <ul>
        <li align="center">
            <img src="img/alimt.png" />
        </li>
        <li align="center">
            <img src="img/nlp.png" />
        </li>
    </ul>
    <div class="arrows">
        <label for="slides_1"></label>
        <label for="slides_2"></label>
    </div>
</div>

CSS

.csslider {
				-moz-perspective: 1300px;
				-ms-perspective: 1300px;
				-webkit-perspective: 1300px;
				perspective: 1300px;
				display: inline-block;
				text-align: left;
				position: relative;
				margin-bottom: 22px;
			}
			
			.csslider>input {
				display: none;
			}
			
			.csslider>input:nth-of-type(10):checked~ul li:first-of-type {
				margin-left: -900%;
			}
			
			.csslider>input:nth-of-type(9):checked~ul li:first-of-type {
				margin-left: -800%;
			}
			
			.csslider>input:nth-of-type(8):checked~ul li:first-of-type {
				margin-left: -700%;
			}
			
			.csslider>input:nth-of-type(7):checked~ul li:first-of-type {
				margin-left: -600%;
			}
			
			.csslider>input:nth-of-type(6):checked~ul li:first-of-type {
				margin-left: -500%;
			}
			
			.csslider>input:nth-of-type(5):checked~ul li:first-of-type {
				margin-left: -400%;
			}
			
			.csslider>input:nth-of-type(4):checked~ul li:first-of-type {
				margin-left: -300%;
			}
			
			.csslider>input:nth-of-type(3):checked~ul li:first-of-type {
				margin-left: -200%;
			}
			
			.csslider>input:nth-of-type(2):checked~ul li:first-of-type {
				margin-left: -100%;
			}
			
			.csslider>input:nth-of-type(1):checked~ul li:first-of-type {
				margin-left: 0%;
			}
			
			.csslider>ul {
				position: relative;
				width: 420px;
				height: 300px;
				z-index: 1;
				font-size: 0;
				line-height: 0;
				margin: 0 auto;
				padding: 0;
				overflow: hidden;
				white-space: nowrap;
				-moz-box-sizing: border-box;
				-webkit-box-sizing: border-box;
				box-sizing: border-box;
			}
			
			.csslider>ul>li {
				position: relative;
				display: inline-block;
				width: 100%;
				height: 100%;
				overflow: hidden;
				font-size: 15px;
				font-size: initial;
				line-height: normal;
				-moz-transition: all 0.5s cubic-bezier(0.4, 1.3, 0.65, 1);
				-o-transition: all 0.5s ease-out;
				-webkit-transition: all 0.5s cubic-bezier(0.4, 1.3, 0.65, 1);
				transition: all 0.5s cubic-bezier(0.4, 1.3, 0.65, 1);
				-moz-background-size: cover;
				-o-background-size: cover;
				-webkit-background-size: cover;
				background-size: cover;
				vertical-align: top;
				-moz-box-sizing: border-box;
				-webkit-box-sizing: border-box;
				box-sizing: border-box;
				white-space: normal;
			}
			
			.csslider>ul>li.scrollable {
				overflow-y: scroll;
			}
			
			
			
			.csslider>.arrows {
				-webkit-touch-callout: none;
				-webkit-user-select: none;
				-khtml-user-select: none;
				-moz-user-select: none;
				-ms-user-select: none;
				user-select: none;
			}
			
			
			.csslider>.arrows {
				position: absolute;
				left: -31px;
				top: 50%;
				width: 100%;
				height: 26px;
				padding: 0 31px;
				z-index: 0;
				-moz-box-sizing: content-box;
				-webkit-box-sizing: content-box;
				box-sizing: content-box;
			}
			
			.csslider>.arrows label {
				display: none;
				position: absolute;
				top: -50%;
				padding: 13px;
				box-shadow: inset 2px -2px 0 1px #3a3a3a;
				cursor: pointer;
				-moz-transition: .15s;
				-o-transition: .15s;
				-webkit-transition: .15s;
				transition: .15s;
			}
			
			.csslider>.arrows label:hover {
				box-shadow: inset 3px -3px 0 2px #71ad37;
				margin: 0 0px;
			}
			
			.csslider>.arrows label:before {
				content: '';
				position: absolute;
				top: -100%;
				left: -100%;
				height: 300%;
				width: 300%;
			}
			
			.csslider.infinity>input:first-of-type:checked~.arrows label:last-of-type,
			.csslider>input:nth-of-type(1):checked~.arrows label:nth-of-type(0),
			.csslider>input:nth-of-type(2):checked~.arrows label:nth-of-type(1),
			.csslider>input:nth-of-type(3):checked~.arrows label:nth-of-type(2),
			.csslider>input:nth-of-type(4):checked~.arrows label:nth-of-type(3),
			.csslider>input:nth-of-type(5):checked~.arrows label:nth-of-type(4),
			.csslider>input:nth-of-type(6):checked~.arrows label:nth-of-type(5),
			.csslider>input:nth-of-type(7):checked~.arrows label:nth-of-type(6),
			.csslider>input:nth-of-type(8):checked~.arrows label:nth-of-type(7),
			.csslider>input:nth-of-type(9):checked~.arrows label:nth-of-type(8),
			.csslider>input:nth-of-type(10):checked~.arrows label:nth-of-type(9),
			.csslider>input:nth-of-type(11):checked~.arrows label:nth-of-type(10) {
				display: block;
				left: 0;
				-moz-transform: rotate(45deg);
				-ms-transform: rotate(45deg);
				-o-transform: rotate(45deg);
				-webkit-transform: rotate(45deg);
				transform: rotate(45deg);
			}
			
			.csslider.infinity>input:last-of-type:checked~.arrows label:first-of-type,
			.csslider>input:nth-of-type(1):checked~.arrows label:nth-of-type(2),
			.csslider>input:nth-of-type(2):checked~.arrows label:nth-of-type(3),
			.csslider>input:nth-of-type(3):checked~.arrows label:nth-of-type(4),
			.csslider>input:nth-of-type(4):checked~.arrows label:nth-of-type(5),
			.csslider>input:nth-of-type(5):checked~.arrows label:nth-of-type(6),
			.csslider>input:nth-of-type(6):checked~.arrows label:nth-of-type(7),
			.csslider>input:nth-of-type(7):checked~.arrows label:nth-of-type(8),
			.csslider>input:nth-of-type(8):checked~.arrows label:nth-of-type(9),
			.csslider>input:nth-of-type(9):checked~.arrows label:nth-of-type(10),
			.csslider>input:nth-of-type(10):checked~.arrows label:nth-of-type(11),
			.csslider>input:nth-of-type(11):checked~.arrows label:nth-of-type(12) {
				display: block;
				right: 0;
				-moz-transform: rotate(225deg);
				-ms-transform: rotate(225deg);
				-o-transform: rotate(225deg);
				-webkit-transform: rotate(225deg);
				transform: rotate(225deg);
			}
			/*#region MODULES */
			/*#endregion */
			
			ul,
			ol {
				padding-left: 40px;
				background-color: white;
			}
			
			html,
			body {
				height: 100%;
				text-align: center;
				font: 400 100% 'Raleway', 'Lato';
			}
			
			.scrollable p {
				padding: 30px;
				text-align: justify;
				line-height: 140%;
				font-size: 120%;
			}
			
			#hewenqislider a {
				color: greenyellow
			}
			
			.csslider>ul {
				width: 375px;
				/*在这里改变宽度 何问起*/
			}

二、自动切换

html

<div id="frame" >
    <div id="photos" class="play">
        <img src="img/alimt.png" >
        <img src="img/mtsaiAI.png" >
    </div>
</div>

CSS

#frame{position:absolute;width:300px;height:200px;overflow:hidden;border-radius:5px}
#dis{position:absolute;left:-50px;top:-10px;opacity:.5}
#dis li{display:inline-block;width:200px;height:20px;margin:0 50px;float:left;text-align:center;color:#fff;border-radius:10px;background:#000}
#photos img{float:left;width:300px;height:200px}
#photos {  position: absolute;z-index:9;  width: calc(300px * 2);/*---修改图片数量的话需要修改下面的动画参数*/  }
.play{ animation: ma 10s ease-out infinite alternate;}
@keyframes ma {
        0%,45% {        margin-left: 0px;       }
        50%,100% {       margin-left: -300px;    }
    }

二、自动切换

CSS图片切换页面效果可以通过使用CSS动画来实现。下面是一个简单的例子: HTML代码: ```html <div class="image-slider"> <input type="radio" name="slide" id="slide-1" checked> <input type="radio" name="slide" id="slide-2"> <input type="radio" name="slide" id="slide-3"> <div class="slides"> <div class="slide1"></div> <div class="slide2"></div> <div class="slide3"></div> </div> <div class="controls"> <label for="slide-1"></label> <label for="slide-2"></label> <label for="slide-3"></label> </div> </div> ``` CSS代码: ```css .image-slider { position: relative; width: 100%; max-width: 600px; margin: 0 auto; } .slides { position: relative; } .slides > div { position: absolute; top: 0; left: 0; width: 100%; height: 300px; opacity: 0; transition: opacity 0.5s; } #slide-1:checked ~ .slides .slide1, #slide-2:checked ~ .slides .slide2, #slide-3:checked ~ .slides .slide3 { opacity: 1; } .controls { position: absolute; bottom: 10px; width: 100%; text-align: center; } .controls label { display: inline-block; width: 15px; height: 15px; background: #333; border-radius: 50%; margin: 0 5px; cursor: pointer; } #slide-1:checked ~ .controls label:nth-child(1), #slide-2:checked ~ .controls label:nth-child(2), #slide-3:checked ~ .controls label:nth-child(3) { background: #fff; } ``` 这段代码中,我们使用了radio input来控制图片切换,每个radio input对应一个图片,通过CSS选择器和:checked伪类来控制当前需要显示的图片。同时,添加了一组控制按钮,通过点击按钮来切换不同的图片。这样就实现CSS图片切换页面效果。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值