javascript - 图像在轮播中组合

标签 javascript html css

图像在轮播中组合。 我用 html 写成

$(function(){
    $('#carousel').each(function(){
        var slideTime = 200;
        var delayTime = 2000;

        var carouselWidth = $(this).width();
        var carouselHeight = $(this).height();
        $(this).append('<div id="carousel_prev"></div><div id="carousel_next"></div>');
        $(this).children('ul').wrapAll('<div id="carousel_wrap"><div id="carousel_move"></div></div>');

        $('#carousel_wrap').css({
            width: (carouselWidth),
            height: (carouselHeight),
            position: 'relative',
            overflow: 'hidden'
        });

        var listWidth = parseInt($('#carousel_move').children('ul').children('li').css('width'));
        var listCount = $('#carousel_move').children('ul').children('li').length;

        var ulWidth = (listWidth)*(listCount);

        $('#carousel_move').css({
            top: '0',
            left: -(ulWidth),
            width: ((ulWidth)*3),
            height: (carouselHeight),
            position: 'absolute'
        });

        $('#carousel_wrap ul').css({
            width: (ulWidth),
            float: 'left'
        });
        $('#carousel_wrap ul').each(function(){
            $(this).clone().prependTo('#carousel_move');
            $(this).clone().appendTo('#carousel_move');
        });

        carouselPosition();

        $('#carousel_prev').click(function(){
            clearInterval(setTimer);
            $('#carousel_move:not(:animated)').animate({left: '+=' + (listWidth)},slideTime,function(){
                carouselPosition();
            });
            timer();
        });

        $('#carousel_next').click(function(){
            clearInterval(setTimer);
            $('#carousel_move:not(:animated)').animate({left: '-=' + (listWidth)},slideTime,function(){
                carouselPosition();
            });
            timer();
        });

        function carouselPosition(){
            var ulLeft = parseInt($('#carousel_move').css('left'));
            var maskWidth = (carouselWidth) - ((listWidth)*(listCount));
            if(ulLeft == ((-(ulWidth))*2) || ulLeft == ((-(ulWidth))*2)+1) {
                $('#carousel_move').css({left:-(ulWidth)});
            } else if(ulLeft == 0) {
                $('#carousel_move').css({left:-(ulWidth)});
            };
        };

        timer();

        function timer() {
            setTimer = setInterval(function(){
                $('#carousel_next').click();
            },delayTime);
        };

    });
});
* {
  margin: 0;
  padding: 0;
}

.menu {
  background-color: gray;
  width: 500px;
  height: 100%;
  float: left;
}

.title {
  color: white;
  font-size: 30px;
  margin: 0px 5px;
  margin-left: 40px;
  text-align: center;
}

.first {
  padding-top: 30px;
}

html,
body,
.menu {
  height: 100%;
}

body {
  margin: 0;
}

.relative {
  position: relative;
}

li {
  list-style: none;
}

li a {
  display: block;
  text-decoration: none;
  color: white;
  text-align: center;
  font-size: 20px;
}

li a img {
  width: 100%;
  height: 100%;
}

.float-right {
  width: calc(100% - 500px);
  overflow: hidden;
  float: right;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.main-container {
  width: 100%;
  float: left;
}

.header {
  float: left;
  width: 100%;
}

.imgArea {
  float: left;
  width: 75%;
  height: 100%;
}
<link rel="stylesheet" href="home.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>

<div class="menu" id="menu">
  <h2 class="title first">SITE</h2>
  <ul class="">
    <li><a class="home" href="">Home</a></li>
    <li><a class="profile" href="">Detail</a></li>
  </ul>
</div>
<div class="imgArea float-right" id="carousel">
  <ul>
    <li><a href="#"><img src="photo1.jpg" alt=""></a></li>
    <li><a href="#"><img src="photo2.jpg" alt=""></a></li>
  </ul>
</div>

<script src="carousel.js"></script>

但是现在当我运行我的网站时,就像 enter image description here

我想制作一个旋转木马,首先显示 photo1,其次显示 photo2,然后再次显示下一张 photo1 ...。但现在首先『photo1 和 photo2 同时显示』其次』它们同时再次显示,接下来它们又是...。为什么我的代码是错误的?我应该如何解决这个问题?我想把 carousel 放在浏览器剩余的区域中,并且 carousel 的图像大小在该区域调整 100%。

最佳答案

我不知道你为什么有这么多代码,如果你使用的是 Bootstrap ,你可以使用你的轮播,这里有一个简单明了的例子。

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Carousel Example</h2>  
  <div id="myCarousel" class="carousel slide" data-ride="carousel">
    <!-- Indicators -->
    <ol class="carousel-indicators">
      <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
      <li data-target="#myCarousel" data-slide-to="1"></li>
      <li data-target="#myCarousel" data-slide-to="2"></li>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner">
      <div class="item active">
        <img src="https://placeimg.com/640/480/arch" alt="Arch" style="width:100%;">
      </div>

      <div class="item">
        <img src="https://placeimg.com/640/480/nature" alt="Nature" style="width:100%;">
      </div>
    
      <div class="item">
        <img src="https://placeimg.com/640/480/tech" alt="Tech" style="width:100%;">
      </div>
    </div>

    <!-- Left and right controls -->
    <a class="left carousel-control" href="#myCarousel" data-slide="prev">
      <span class="glyphicon glyphicon-chevron-left"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#myCarousel" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
</div>

</body>
</html>

关于javascript - 图像在轮播中组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47994311/

相关文章:

html - 如何在底部像镜子一样反射heder文本

javascript - 单击时 Bootstrap 日历日期时间选择器不会加载

JavaScript,MooTools - Class.Event - onComplete : Do nothing by default

php - 如何在 jQuery 中编辑显示模式框的链接

java - 如何更改 JLabel 中特定字符的颜色?

html - 在 HTML 中设置图像尺寸

html - 响应式 div 元素内的表格(导致调整窗口重叠)

javascript - 在图像加载之前显示替代文本代替图像

javascript - 我可以向从代码触发的事件添加新属性吗

html - div类显示:none not working