html - 如何使用 HTML 和 CSS 为 div 标签使用滑块选项
问题描述
我列出了两个 div 标签 Nut 1 和 Nut 2,我试图添加一个滑块,使 div 标签水平移动。我尝试了很多教程,但我做不到。而且我不确定滑块是否也可以像页面的部分一样响应。只有图像我能够制作一个滑块,但我不能制作其他内容。
HTML
<div class="slider">
<!-- Nut 1-->
<figure class="images_in_nuts">
<img src="images/almonds.jpg" />
</figure>
<div class="nut_text">
<h2>Almond</h2>
<h3 class="rich_in">Rich In</h3>
<p>
Vitamin E, Monounsaturated fats, Fiber, Biotin, <br />Calcium,
Phosphorus, Magnesium, Copper, <br />
Phytonutrients, specifically flavonoids, plant sterols,<br />
phenolic acids
</p>
</div>
<!-- Nut 1 Ends-->
<!-- Nut 1-->
<figure class="images_in_nuts">
<img src="images/almonds.jpg" />
</figure>
<div class="nut_text">
<h2>Almond</h2>
<h3 class="rich_in">Rich In</h3>
<p>
Vitamin E, Monounsaturated fats, Fiber, Biotin, <br />Calcium,
Phosphorus, Magnesium, Copper, <br />
Phytonutrients, specifically flavonoids, plant sterols,<br />
phenolic acids
</p>
</div>
<!-- Nut 1 Ends-->
</div>
CSS
.slider {
overflow: hidden;
position: relative;
background: #ebebeb;
display: flex;
justify-content: center;
border: 5px solid lightgrey;
margin: 20px;
animation: slider 20s infinite;
float: inline-end;
}
.images_in_nuts {
margin-top: 10px;
width: 250px;
height: 250px;
margin-bottom: 10px;
}
.nut_text {
text-align: center;
font-family: "Poppins";
margin-top: 50px;
margin-left: 20px;
}
.rich_in {
text-align: center;
}
我添加了一个指向笔代码的链接https://codepen.io/gladwin-james/pen/qBqRxKw
解决方案
我不确定这是否是您想要实现的幻灯片。但是,我举了一个例子来说明我认为您正在尝试实现的内容:
这是我从 www3schools.com 使用的代码 如果您重新使用我的示例 CSS,您应该会很高兴:
* {box-sizing: border-box;}
body {font-family: Verdana, sans-serif;}
.mySlides {display: none;}
img {vertical-align: middle;}
/* Slideshow container */
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
/* Caption text */
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}
/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
/* The dots/bullets/indicators */
.dot {
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active {
background-color: #717171;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
.text {font-size: 11px}
}
HTML:
<div class="slideshow-container">
<div class="mySlides fade">
<div class="numbertext">1 / 3</div>
<img src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_960_720.jpg" style="width:100%">
<div class="text">
<h2>Almond</h2>
<h3>Rich In</h3>
Vitamin E, Monounsaturated fats, Fiber, Biotin, <br />Calcium,
Phosphorus, Magnesium, Copper, <br />
Phytonutrients, specifically flavonoids, plant sterols,<br />
phenolic acids</div>
</div>
<div class="mySlides fade">
<div class="numbertext">2 / 3</div>
<img src="https://cdn.pixabay.com/photo/2014/02/27/16/10/tree-276014_960_720.jpg" style="width:100%">
<div class="text"> <h2>Almond</h2>
<h3>Rich In</h3>
Vitamin E, Monounsaturated fats, Fiber, Biotin, <br />Calcium,
Phosphorus, Magnesium, Copper, <br />
Phytonutrients, specifically flavonoids, plant sterols,<br />
phenolic acids</div></div>
</div>
<div class="mySlides fade">
<div class="numbertext">3 / 3</div>
<img src="https://cdn.pixabay.com/photo/2014/04/14/20/11/flowers-324175_960_720.jpg" style="width:100%">
<div class="text"> <h2>Almond</h2>
<h3>Rich In</h3>
Vitamin E, Monounsaturated fats, Fiber, Biotin, <br />Calcium,
Phosphorus, Magnesium, Copper, <br />
Phytonutrients, specifically flavonoids, plant sterols,<br />
phenolic acids</div></div>
</div>
</div>
<br>
<div style="text-align:center">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
Javascript(记住脚本标签):
var slideIndex = 0;
showSlides();
function showSlides() {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slideIndex++;
if (slideIndex > slides.length) {slideIndex = 1}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
setTimeout(showSlides, 2000); // Change image every 2 seconds
}
推荐阅读
- r - R中的Choroplet映射
- javascript - 如何避免nodejs循环上的429
- php - 如何通过请求phpunit发送对象
- mysql - mysql 语法查询.difference between have 和 where 有什么区别
- javascript - 循环中的更新选项卡在 Chrome 扩展扩展中不起作用
- autosar - AUTOSAR的实现规则?
- flutter - 如果启用了 nullsafety,如何在颤振复选框中管理三态
- c# - Linq 到实体可以为空的 int 列需要吗?
- python - django模板使用模板变量作为索引获取列表项
- javascript - Javascript/HTML - 隐藏 DIV 直到单击按钮