繁体   English   中英

防止图像幻灯片比容器单元增长更多

[英]Prevent image slideshow grows more than container cell

我正在制作一个小的html-jquery程序,它动态生成一个表,每个单元格上都有一个幻灯片 幻灯片显示从计算机内的文件夹中选择的图像。

问题是,当我选择文件夹时,幻灯片显示每个单元格的幻灯片高度达到100vh 我无法理解如何防止幻灯片比包含单元格增长更多。

HTML

<body style="height:100vh;">
    <div class="row">
      <div class="container-fluid">
        Rows: <input id="sRows" type="number" name="" value="2">
        Columns: <input id="sCol" type="number" name="" value="2">
        <button id="btnGen" type="button" name="button">Generate!</button>
      </div>
    </div>
    <div  id="slideshowContiner" class="w3-table-all" >
    </div>
</body>

CSS

  #slideshowContiner {
        table-layout: fixed;
        height: 100%;
      }
      .slider{
        max-width:100%;
        max-height: 100%;
      }
      .mySlide{
        width:100%;
        height:auto;
      }

对于Javascript代码可能太长而无法读取,最后创建的结构是, #slideshowContiner内部是这样的:

<div id="slideshowContiner" class="w3-table-all">
  <tr></tr>
  <td style="background-color:#6a833c">
    <input webkitdirectory="" mozdirectory="" msdirectory="" odirectory="" directory="" multiple="" type="file" name="file">
    <div class="w3-content w3-section slider" style="">
      <img class="mySlide" style="display: none;" src="blob:null/">
      <img class="mySlide" style="display: none;" src="blob:null/">
      <img class="mySlide" style="display: none;" src="blob:null/">
    </div>
    </td>
  </div>

Jquery(TL:DR;)

$( document ).ready(function() {
      var randomColor = function (){
          return '#'+ ('000000' + Math.floor(Math.random()*16777215).toString(16)).slice(-6);
      }

      var sliders = []
      var myIndex = 0;

      function carousel() {
        var i;
        var slides = elem.children( ".mySlide" ).css("display", "none")
        console.log(slides, this);
      }
      function createSlider(files){
        var slider = $('<div class="w3-content w3-section slider" style=""></div>')
        for (var i = 0; i < files.length; i++) {
          prev = $('<img class="mySlide" style="">').attr("src", URL.createObjectURL(files[i]))
          slider.append(prev)
        }
        sliders.push(slider)
        // slider.data("id", inn++)
        var myIndex = 0;
        slider.data("slideshow", function(){
          console.log(myIndex);
          var slides = slider.children( ".mySlide" ).css("display", "none")
          myIndex++;
          if (myIndex > slides.length) {myIndex = 1}
          console.log(slides[myIndex]);
          $(slides[myIndex-1]).css("display", "block")
          setTimeout(slider.data("slideshow"), 2000)

        })
        return slider
      }

      var sContainer = $("#slideshowContiner")
      var sRows = $("#sRows")
      var sCol = $("#sCol")
      var btnGen = $("#btnGen")

      function genGrid(){
        sContainer.html("")
        for (var i = 0; i < sRows.val(); i++) {
          var newRow = sContainer.append('<tr></tr>')
          // var newRow = sContainer.append('<div class="row flex-fill d-flex " style="border: 1px solid;"></div>')
          for (var x = 0; x < sCol.val(); x++) {
            var newCol = $('<td style="background-color:'+randomColor()+'"></td>')
            newRow.append(newCol);
            var input = $('<input webkitdirectory mozdirectory msdirectory odirectory directory multiple/>')
                .attr('type', "file")
                .attr('name', "file")
                .on("change", function() {
                  cell = $(this).data("cell")
                  var s = createSlider(this.files)
                  cell.append(s)
                  // carousel(s)
                  var init = s.data("slideshow")();

                })
                .data("cell", newCol);;
            newCol.append($(input))
          }
        }
      }
      genGrid()
      btnGen.click(genGrid)

JsFiddle

注意:在这个小提琴上style="height:100vh;" 在根div上,原始代码在body标签上。

您可以使用类中的静态单位(如px将每个图像设置为固定高度,并使用一些媒体查询来控制不同分辨率下的尺寸。 通过这种方式,您可以期待它们的外观。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM