中心prev next按钮垂直响应

时间:2017-09-25 16:34:31

标签: image button slider gallery

我正在创建一个图像滑块,一切都可以工作到目前为止,但我的上一个和下一个按钮没有垂直居中在主图像上。我也试图让它变得敏感。

我尝试了一切,但我不知道我错过了什么。顶部:50%的绝对位置似乎没有成功。这是codepen

$('#imgDetail li img').click(function(){
    $('#unidoor').attr('src',$(this).attr('src'));
});
$('#next').on('click',function(){
 var imgSrc = $('#unidoor').attr('src');
    var nextSrc = $('ul img[src="'+imgSrc+'"]').closest('li').next().find('img').attr('src');
    console.log(nextSrc);
    nextSrc ==undefined?$('#unidoor').attr('src',$('ul img:first').attr('src')): $('#unidoor').attr('src',nextSrc);
});
$('#prev').on('click',function(){
 var imgSrc = $('#unidoor').attr('src');
    var nextSrc = $('ul img[src="'+imgSrc+'"]').closest('li').prev().find('img').attr('src');
    console.log(nextSrc);
    nextSrc ==undefined?$('#unidoor').attr('src',$('ul img:last').attr('src')): $('#unidoor').attr('src',nextSrc);
});
* {
  margin: 0;
  padding: 0;
}

body{
  margin: 0;
  padding:0;
  font-size: 100%;
/*   line-height: 1.6; */
/*   font-family: Arial, Helvetica, sans-serif; */
}

.header{
  margin: 0 auto;
  width: 100%;
  background-color: #333;
  padding: 30px 0 0 0; 
}

.header h1{
  margin: 0;
  text-align: center;
  color: white;
  font-family: Arial, Helvetica, sans-serif;
}

.header ul {
  list-style-type: none;
  margin: 0;
/*   padding: 0; */
  overflow: hidden;
  padding: 20px 0px 30px 0;
  text-align: center;
}

.header li {
  display: block;
  display: inline-block;
/*   border-right: 1px solid #bbb; */
  border-right: 1px solid #bbb;
  height: 25px;
}

.header li:last-child{
  border-right: none;
}

.header li a {
  display: block;
  color: white;
  text-align: center;
  text-decoration: none;
  padding: 0px 40px;
  font-size: 1em;
}

.header li a:hover{
  color: #7bbe9a;
/*   color: #80b198; */
}

#green-room {
  background: #333 !important;
}

#unidoor {
  position: relative;
  width: 90%;
  margin: 0 auto;
  display: block;
}

#prev {
  position: absolute;
  top: 50%;
  left: 10%;
  transform: translate(-10%, -50%);
}

#next {
  position: absolute;
  top: 50%;
  right: 10%;
  transform: translate(-10%, -50%);
}

/* .previous {left: 10%;}
.next {right: 10%;} */

/* .prev-next-button a {
  position: absolute;
  left: 20%;
  top: 20%;
} */

#imgDetail ul {
  margin: 0 auto;
  display: block;
  width: 50%;
}

.thumb { 
  width: 25%; 
  height: auto; 
  margin: 15px 5px 0 5px;
}

#imgDetail li { 
  display: inline; margin-right: 10px; 
}

#imgDetail a {
    text-decoration: none;
    display: inline-block;
    padding: 8px 16px;
}

#imgDetail a:hover {
  background-color: #7bbe9a;
  color: white;
  opacity: 1;
}

.previous {
  background-color: #fff;
  opacity: 0.5;
  color: black;
}

.next {
  background-color: #fff;
  opacity: 0.5;
  color: black;
}

/* .round {
  border-radius: 50%;
} */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!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">
    <title>Daniel Pollack</title>
    <link rel="stylesheet" type="text/css" href="css/styles.css"/>
  </head>

  <body id="green-room">
  	<div class="header">
      <div id="title"><h1>Lorem Ipsum 3D Online Portfolio</h1></div>
      <nav id="menu">
        <ul>
          <li><a href="index.html">Home</a></li>
          <li><a href="index.html#portfolio">Portfolio</a></li>
          <li><a href="#about">About</a></li>
          <li><a href="#contact">Contact</a></li>
        </ul>
      </nav>
    </div>

<div id="imgDetail">
  <br>
    <img src="http://www.davidwightman.net/_images_landscape/Behemot/Behemot_detail_1.jpg" alt="" id="unidoor" />
  <a href="#" id="prev" class="prev-next-button previous">&#8249;</a>
<a href="#" id="next" class="prev-next-button next">&#8250;</a>
    <ul>
      <li><img src="http://www.davidwightman.net/_images_landscape/Behemot/Behemot_detail_1.jpg" class="thumb" /></li>
      <li><img src="http://www.davidwightman.net/_images_landscape/Behemot/Behemot_detail_2.jpg"  class="thumb" /></li>
      <li><img src="http://www.davidwightman.net/_images_landscape/Behemot/Behemot_detail_3.jpg" class="thumb" /></li>
    </ul>
</div>
    
    <script src="https://unpkg.com/scrollreveal/dist/scrollreveal.min.js"></script>
    <script>
      window.sr = ScrollReveal({reset: true});
      sr.reveal('#unidoor');
    </script>
    
    </body>

</html>

1 个答案:

答案 0 :(得分:0)

你好,你可以将<div id="imgDetail">包装在例如<div class="slideshow-container">的外部div类中,然后添加你看起来像这样的CSS:

.slideshow-container {
  max-width: 1000px;
  position: relative;
  margin: auto;
}

您可以在codepen上获得更改。快乐的编码:)