如何在轮播中水平显示两个div?

时间:2018-09-11 15:52:00

标签: javascript jquery html css

/*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>

我要在幻灯片显示中一排显示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>

with the code edited

2 个答案:

答案 0 :(得分:1)

我添加了一个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%的宽度。

我还做了其他一些小的修改,但这些修改大多是装饰性的。

我希望这是有用的。欢迎提问:-)

View Codepen

答案 1 :(得分:1)

代码问题是错误的属性-float: flex,应为display: flex,您不能将其设置为#slideshow,因为javascript更新会将其更改为{{1 }}。