/*slides show*/
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 = "flex";
dots[slideIndex-1].className += " active";
setTimeout(showSlides, 5000); // Change image every 2 seconds
}
/*End slides show*/
img {vertical-align: middle;}
/* Slideshow container */
.mySlides{
display: flex;
flex-direction: row;
width: 75vw;
border: 1px solid red;
}
.mySlides > div {
flex: 0 0 50%;
border: 1px solid green;
}
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
margin-top: 50px;
}
/* Caption text */
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
box-sizing: border-box;
}
/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
}
/* The dots/bullets/indicators */
.dot {
height: 5px;
width: 5px;
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}
}
<body>
<div id="header" class="navbar">
<div id="Title">
<a href="Accueil"><img src="IMAGES/PNG/logo.png" alt="logo" /></a>
</div>
<div id="navbar">
<div class="menu"> <a class="active" href="javascript:void(0)">Blog</a></div>
<div class="menu"> <a href="blog">Contact</a></div>
<div class="menu"> <a href="blog">L'électrophotonique</a></div>
<div class="menu"> <a href="blog">Qui sommes nous?</a></div>
</div>
</div>
<div class="slideshow-container">
<div id = "slideshow" class="mySlides fade">
<div><img src="IMAGES/PNG/background.png"></div>
<div class="text"> test1 </div>
</div>
<div class="mySlides fade">
<img src="IMAGES/PNG/eau59h.png">
<div class="text">test 2</div>
</div>
<div class="mySlides fade">
<img src="IMAGES/PNG/pont.png">
<div class="text">test 3</div>
</div>
</div>
<br>
<div style="text-align:center">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
<div class="content" >
<script type="text/javascript" src="JS/sticky_navbar.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>
<script src="js/index.js"></script>
我想在我的幻灯片放映中的 1 行中显示 2 个 div? 一部分是图像,第二部分是图例和文本。
我正在尝试使用 flexbox 来做到这一点,但它不起作用,我不明白为什么。
我引入了一个带有 ID(幻灯片)的 div 进行测试,但是带有图像的 div 和 div class = "text"仍然垂直对齐
你能帮帮我吗?
/*slides show*/
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, 5000); // Change image every 2 seconds
}
/*End slides show*/
@charset "UTF-8";
/* CSS Document */
body {
margin: 0;
font-size: 28px;
background-color: #00011f;
display: flex;
flex-direction: column;
margin : auto;
}
img {vertical-align: middle;}
/* Slideshow container */
#slideshow{
float: flex;}
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
margin-top: 50px;
}
/* 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: 5px;
width: 5px;
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 */
@media only screen and (max-width: 300px) {
.text {font-size: 11px}
}
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="CSS/style2.css" />
<link rel="shortcut icon" href="IMAGES/PNG/favicon.png" />
<head>
<meta charset="utf-8" />
<title>Electrophotonique Ingenierie</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="slideshow-container">
<div id = "slideshow" class="mySlides fade">
<div><img src="IMAGES/PNG/background.png"></div>
<div class="text"> testtesttesttesttesttesttesttesttesttesttesttest </div>
</div>
<div class="mySlides fade">
<img src="IMAGES/PNG/eau59h.png" style="width:100%">
<div class="text"></div>
</div>
<div class="mySlides fade">
<img src="IMAGES/PNG/pont.png" style="width:100%">
<div class="text"></div>
</div>
</div>
<br>
<div style="text-align:center">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
<div class="content" ></div>
<script type="text/javascript" src="JS/sticky_navbar.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>
<script src="js/index.js"></script>
</body>
</html>
最佳答案
我添加了一个 Codepen 来解决您的问题,您可以在其中检查我对您的标记所做的更改。
Flexbox 是一种显示布局方法,因此我将 float:flex
替换为 display:flex
。然后我添加了一些进一步的样式规则,导致 2 列布局,每列是 flexbox 容器宽度的 50%(因为必须定义宽度才能工作,我将宽度设置为 75vw,这相当于浏览器屏幕宽度的 75%)。
每个“幻灯片”的标记需要相同才能使布局正常工作,因此我将样式规则 #slideshow
更改为 .mySlides
并确保图像位于 div 标记内,我看到您已将其添加到第一张幻灯片中。
这是所需的样式(设置了一些边框颜色,以便您可以看到发生了什么):
.mySlides{
display: flex;
flex-direction: row;
width: 75vw;
border: 1px solid red;
}
.mySlides > div {
flex: 0 0 50%;
border: 1px solid green;
}
flex: 0 0 50%
是简写,但在这种情况下指示每列至少占据 50% 的宽度。
我做了一些其他的小修改,但这些主要是装饰性的。
我希望这是有用的。欢迎提问:-)
关于javascript - 如何在我的旋转木马中水平显示两个 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52279815/