0

我想动态地制作这个 HTML:图像和这个“点”,我想从 JavaScript 数组中加载它们。请帮忙!

<div id="slider" class="carousel slide" data-ride="carousel"> 
    <ol class="carousel-indicators"> 
        <li data-target="#slider" data-slide-to="0" class="active"></li> 
        <li data-target="#slider" data-slide-to="1"></li> 
        <li data-target="#slider" data-slide-to="2"></li> 
    </ol> 

<div class="carousel-inner"> 
    <div class="item active"> 
        <img src="~/Content/img/sl1.jpg" width="100%" /> 
    </div> 

    <div class="item"> 
        <img src="~/Content/img/sl2.jpg" width="100%" /> 
    </div> 

    <div class="item"> 
        <img src="~/Content/img/3sl.jpg" width="100%" /> 
    </div> 
</div> 

<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>
4

2 回答 2

0

您可以使用 bootstrap 4 和 jquery 来执行此操作

    <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel"></div>

    <script>
    var images = ['//lorempixel.com/300/250/food/4/','//lorempixel.com/300/250/food/2/','//lorempixel.com/300/250/food/3/'];
    buildcarousel("carouselExampleIndicators", images)
    function buildcarousel(id,images){
      var html = $("#"+id).append('<ol class="carousel-indicators"></ol><div class="carousel-inner"></div><a class="carousel-control-prev" href="#'+id+'" role="button" data-slide="prev"><span class="carousel-control-prev-icon" aria-hidden="true"></span><span class="sr-only">Previous</span></a><a class="carousel-control-next" href="#'+id+'" role="button" data-slide="next"><span class="carousel-control-next-icon" aria-hidden="true"></span><span class="sr-only">Next</span></a>');
      let indicators = html.find('.carousel-indicators');
      let carousel = html.find('.carousel-inner')
      images.forEach((e,i)=>{
        var activeclass = i == 0 ? "active":"";
        indicators.append('<li data-target="#carouselExampleIndicators" data-slide-to="'+i+'" class="'+activeclass+'"></li>');
        carousel.append('<div class="carousel-item '+activeclass+'"><img class="d-block w-100" src="'+e+'" alt="First slide"></div>');
      })
    }
    </script>

演示链接:https ://codepen.io/manab_27/pen/pMjROY

于 2019-07-22T09:54:38.900 回答
0

好吧对不起试试这个:

   <script language='javascript'>
    var img = ['~/Content/img/sl1.jpg', '~/Content/img/sl2.jpg',  '~/Content/img/sl3.jpg'];

  function viewImage(id, next) {
    let imgElement = document.getElementById(id);

    let index = imgElement.getAttribute('index');
        if (typeof index === 'undefined') {
        index = 0;
    }

    if(next === 'next') {
        index++;
      if (index == img.length) index = 0;
    }
    else{
        index--;
      if (index < 0) index = img.length - 1;
    }

    imgElement.setAttribute('index', index);
    imgElement.src = img[index];
  }

</script>



<div>
  <button onclick="viewImage('image','previous')">PREVIOUS
  </button>
  <button onclick="viewImage('image','next')">NEXT
  </button>
</div>

<div>
  <img id="image" src=""/>
</div>
于 2018-06-05T15:57:27.217 回答