css实现点击内容切换div

相信很多小伙伴都看过,点击切换内容技术,今天我们简单说一下在不用js的情况下实现内容切换。

html代码:

css代码:

 完成效果:

 源代码如下:

有用的到的地方可以直接复制代码,可以直接使用哦~也可以用来点击切换照片,只需要把,下面div里面文字内容换成img就可以了.

首先要设置两个按钮botton,再在按钮下设置相对应的框,框一和框二

 <div class="main">
            <p class="qr"><i class="iconfont icon-saomiaoerweima"></i></p>
            <input type="radio" name="gender" id="bro1" />
            <label for="bro1">登录</label>
            <div class="main-move">
                框一
               
            </div>
            <input type="radio" name="gender" id="bro2" checked />
            <label for="bro2">注册</label>
            <div class="main-move">
               框二
            </div>

 

css代码如下:

首先,设置外层wrapper宽高、背景验证、水平居中、相对定位

再设置内层div相对于父级绝对定位,默认隐藏 

 再设置label宽高、行内块、文本居中、行高、字体大小、字体颜色

然后默认所有radio隐藏

再被选中的radio下的label下的div显示

最后被选中的radio下的label

/* 设置外层main宽高 */
.main {
    width: 450px;
    height: 570px;
    position: relative;
    top: 150px;
    left: 120px;
    box-shadow: 2px 2px 2px rgb(249, 249, 249);
    margin: 0 auto;
}

/* 设置内层div相对于父级绝对定位,默认隐藏 */
.main div {
    display: none;
    position: absolute;
    top: 50px;
}

/* 设置label宽高、行内块、文本居中、行高、字体大小、字体颜色 */
label {
    width: 100px;
    height: 50px;
    display: inline-block;
    text-align: left;
    line-height: 50px;
    font-size: 22px;
    color: black;
}

label::after {
    content: '';
    display: block;
    width: 45px;
    height: 4px;
    background-color: rgb(255, 92, 0);
    position: relative;
    top: -10px;
}
/* 默认所有radio隐藏 */
input[type="radio"] {
    display: none;
}

/* 被选中的radio下的label下的div显示 */
input[type="radio"]:checked+label+div {
    display: block;
}

/* 被选中的radio下的label*/
input[type="radio"]:checked+label {
    font-weight: 900;
}

HTML <label> 标签

<label> 标签为 input 元素定义标注(标记)。

label 元素不会向用户呈现任何特殊效果。不过,它为鼠标用户改进了可用性。如果您在 label 元素内点击文本,就会触发此控件。就是说,当用户选择该标签时,浏览器就会自动将焦点转到和标签相关的表单控件上。

<label> 标签的 for 属性应当与相关元素的 id 属性相同。

支持的浏览器:

 语法

<label for="male">Male</label><input type="radio" name="sex" id="male">

  • 12
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤实现: 1. 在HTML中创建一个包含所有图片的容器,如div,并为其设置固定宽度和高度。 2. 在容器内添加多个img标签,每个标签对应一张图片,并设置它们的display属性为none,以隐藏它们。 3. 创建两个按钮,分别用于向前和向后切换图片,并为它们添加事件监听器。 4. 在事件监听器中,使用JavaScript获取所有img标签,并根据当前显示的图片,切换到下一张或上一张图片。同时,将当前显示的图片的display属性设置为none,将要显示的图片的display属性设置为block。 5. 使用CSS对按钮进行样式设置。 以下是示例代码: HTML: ``` <div class="image-container"> <img src="image1.jpg" alt="Image 1" /> <img src="image2.jpg" alt="Image 2" /> <img src="image3.jpg" alt="Image 3" /> </div> <button class="prev-btn">Prev</button> <button class="next-btn">Next</button> ``` CSS: ``` .image-container { width: 500px; /* 设置容器宽度 */ height: 300px; /* 设置容器高度 */ position: relative; /* 设置相对定位,以便于设置子元素的绝对定位 */ } .image-container img { position: absolute; /* 设置绝对定位 */ top: 0; left: 0; display: none; /* 隐藏所有图片 */ } .prev-btn, .next-btn { position: absolute; top: 50%; /* 将按钮垂直居中 */ transform: translateY(-50%); /* 将按钮向上移动自身高度的一半,以实现垂直居中 */ padding: 10px 20px; background-color: #ccc; border: none; cursor: pointer; } .prev-btn { left: 20px; /* 将“上一张”按钮定位到容器左边缘 */ } .next-btn { right: 20px; /* 将“下一张”按钮定位到容器右边缘 */ } ``` JavaScript: ``` const prevBtn = document.querySelector('.prev-btn'); const nextBtn = document.querySelector('.next-btn'); const images = document.querySelectorAll('.image-container img'); let currentImageIndex = 0; function showImage(index) { images[currentImageIndex].style.display = 'none'; // 隐藏当前图片 images[index].style.display = 'block'; // 显示目标图片 currentImageIndex = index; // 更新当前图片索引 } prevBtn.addEventListener('click', () => { let index = currentImageIndex - 1; if (index < 0) { index = images.length - 1; } showImage(index); }); nextBtn.addEventListener('click', () => { let index = currentImageIndex + 1; if (index >= images.length) { index = 0; } showImage(index); }); showImage(currentImageIndex); // 初始化显示第一张图片 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值