Take images from a folder with javascript and display on a Slide

1

Hello! I have a slide that works normally. But I have to keep passing the link of each image to him.

What I wanted to do is to put a folder path to identify the files in png or jpg and display them automatically with javascript.

The code looks like this:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.7/css/swiper.min.css">

  <style>
    html, body {
      position: relative;
      height: 100%;
    }
    body {
      background: #000;
      font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
      font-size: 14px;
      color:#000;
      margin: 0;
      padding: 0;
    }
    .swiper-container {
      width: 100%;
      height: 100%;
    }
    .swiper-slide {
      overflow: hidden;
    }
  </style>
</head>
<body>
  
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide">
        <div class="swiper-zoom-container">
          <img src="http://lorempixel.com/800/800/sports/1/"></div></div><divclass="swiper-slide">
        <div class="swiper-zoom-container">
          <img src="http://lorempixel.com/800/400/sports/2/"></div></div><divclass="swiper-slide">
        <div class="swiper-zoom-container">
          <img src="http://lorempixel.com/400/800/sports/3/"></div></div></div><divclass="swiper-pagination swiper-pagination-white"></div>
    
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.7/js/swiper.min.js"></script>

  
  <script>
    var swiper = new Swiper('.swiper-container', {
      zoom: true,
      pagination: {
        el: '.swiper-pagination',
      },
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
    });
  </script>
    
asked by anonymous 06.12.2017 / 17:25

1 answer

1

You can loop to insert the images in sequence. All you have to do is set the number of images you want to insert to var num_imagens .

For example, you want to insert 3 images, set var num_imagens = 3; .

See:

var num_imagens = 3;
function carregaImgs(){

   var img_div = document.querySelectorAll(".swiper-wrapper")[0];
   
   for(var x=1;x<=num_imagens;x++){
      var img_container = '<div class="swiper-slide"><div class="swiper-zoom-container">'
      +'<img src="http://lorempixel.com/400/800/sports/'+x+'/">'+'</div></div>';img_div.innerHTML+=img_container;}}window.onload=function(){carregaImgs();varswiper=newSwiper('.swiper-container',{zoom:true,pagination:{el:'.swiper-pagination',},navigation:{nextEl:'.swiper-button-next',prevEl:'.swiper-button-prev',},});}
html, body { position: relative; height: 100%; } body { background: #000; font-family: Helvetica Neue, Helvetica, Arial, sans-serif; font-size: 14px; color:#000; margin: 0; padding: 0; } .swiper-container { width: 100%; height: 100%; } .swiper-slide { overflow: hidden; }
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.7/css/swiper.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.7/js/swiper.min.js"></script><linkrel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.7/css/swiper.min.css">
<div class="swiper-container">
   <div class="swiper-wrapper">
   </div>
   <div class="swiper-pagination swiper-pagination-white"></div>
   <div class="swiper-button-prev"></div>
   <div class="swiper-button-next"></div>
</div>
    
07.12.2017 / 00:24