纯css实现幻灯片 同时 按钮按下变色

本文介绍了两种实现网页按钮点击变色的方法:一种是通过使用a链接锚点结合jQuery来改变按钮颜色;另一种是利用input按钮和:checked选择器实现相同功能。此外还列举了一些常用的CSS属性选择器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法一:运用a链接锚点实现 + jquery实现按钮按下变色

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <script type="text/javascript"src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script><!--导入jquery库-->
  <title></title>
  <style type="text/css">
  	*{
  		padding: 0;
  		margin: 0;
  	}
  	body{
  		padding: 50px;
  	}
  	a{
  		list-style: none;
  		text-decoration: none;
  		color: #000000;
  		display: inline-block;
  		width: 60px;
  		height: 20px;
  		font-size: 14px;
  		text-align: center;
  	}
  	.content{
  		width: 200px;
  		height: 200px;
  		overflow: hidden;
  	}
  	#a1{
  		width: 200px;
  		height: 200px;
  		background: greenyellow;
  	}
  	#a2{
  		width: 200px;
  		height: 200px;
  		background: orange;
  	}
  	.end{
  		background: blue ;
  		color: #fff;
  	}
  </style>
  <script type="text/javascript">
		$(function(){
			  $("a").click(function(){
			    $("a").eq($(this).index()).addClass("end").siblings().removeClass("end");
			  })
			})
	</script>
</head>
<body>
  <div> 
   <a href="#a1"  class="end">第一页</a>
  <a href="#a2">第二页</a>
  <div class="content">
  	<div id="a1">
  		
  	</div>
  	<div id="a2">
  		
  	</div>
  </div>
 
</body>
</html>

方法二:用input按钮和:checked选择器

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
    <title></title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        .tab{
            width: 300px;
            height: 150px;
            margin-left: 30px;
            margin-top: 30px;
            border: 1px solid #eee;
            position: relative;
            overflow: hidden;
        }
        input[type='radio']{
            display: none;
        }
        .tab label{
            display: block;
            cursor: pointer;
            position: absolute;
            width: 60px;
            height: 30px;
            line-height: 30px;
            text-align: center;
            border: 1px solid #eee;
        }
        .one{
            left: 0;
            top: 0;
        }
        .two{
            left: 60px;
            top: 0;
        }
        input[type='radio']:checked~div[class^='mod']{
            display: block;
        }
        input[type='radio']:checked~label{
            background: green;
            color: #fff;
        }
        [class^='mod']{/*[class^='']匹配属性值以指定值开头的每个元素*/
            position: absolute;
            top: 40px;
            left: 20px;
            display: none;
        }
        .mod-1{
        	width: 200px;
        	height: 80px;
        	background: blueviolet;
        }
         .mod-2{
        	width: 200px;
        	height: 80px;
        	background: yellow;
        }
    </style>
</head>
<body>
    <div class="tab">
        <div>
            <input type="radio" id="rd" name="tab" checked>
            <label for="rd" class="one">第一张</label>
            <div class="mod-1">
                
            </div>
        </div>
        <div>
            <input type="radio" id="rds" name="tab">
            <label for="rds" class="two">第二张</label>
            <div class="mod-2">
                
            </div>
        </div>
    </div>
</body>
</html>

补充:还有一些常用的css属性选择器如下:

  • [attribute~=value] 选取属性值中包含指定词汇的元素

  • [attribute|=value]  选取带有以指定值开头的属性值的元素,该值必须是整个单词。

  • [attribute$=value] 匹配属性值以指定值结尾的每个元素

  • [attribute*=value] 匹配属性值中包含指定值的每个元素

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值