自动检测最高图像的高度

时间:2016-02-20 14:51:02

标签: javascript jquery

我正在处理滑动横幅的代码。 https://jsfiddle.net/r4ht890f/

问题1)我不想通过css为此部分设置高度,让javascript代码实际控制部分的高度。是否有一种简单的方法可以添加到javascript代码中,它可以检测到最高图像的高度,并以某种方式设置它以便图像显示在页面上。现在,没有任何东西出现,因为没有检测到高度。

问题2)我想对这个脚本做的唯一另一件事就是让最后一个自然地回到第一个形成一个循环而不是回到所有旧的循环来回到1。

#images_holder {
    float: left;
    width: 100%; 
}
#moving_part {
    width: 100%;

}
#moving_part > div {
    position: relative;
    width: 100%; 
    height: auto;
    float:    left;
}
#moving_part > div img{
    float: left; 
    width:  100%; 
    height: auto;
}
#prev, #next {
    position: absolute;
    cursor:   pointer;
    top:      47%;  
  display:  none; /* hide initially */
    -webkit-user-select: none;
            user-select: none;
}
#next{
    right: 0px;
}
#description{
    color:      #fff;
    background: rgba(0,0,0,0.4);
    text-align: center;
    position:   absolute;
    padding:    15px;
    right:      0;
    left:       0;
  display:    none; /* hide initially */
}

/* EXTRA: navigation */
#navigation{
  display:    none; /* hide initially */
  position:absolute;
    width:100%;
    bottom:0;
  text-align:center
}
#navigation span{ /* created by jQuery*/
    display:inline-block;
  background: rgba(255,255,255,0.6);
  cursor:pointer;
  padding:4px 10px;
}

这里是javascript

jQuery(function( $ ){

    var efx  = "slide", // "slide" || "fade"
            animationTime = 600,
            pauseTime = 4000,
      $gal = $("#images_holder"),
        $mov = $("#moving_part"),
        $sli = $mov.find("> div"),
        $btn = $("#prev, #next"),
            $dsc = $("#description"),
            $nav = $("#navigation"), // Our buttons parent
            $navBtns, // Later, our SPAN buttons
            spans = "",
            w = $gal.width(),
            n = $sli.length,
            c = 0,  // Counter // Start index
            itv;    // Interval


for(var i=0; i<n; i++) {     // `n` is the number of slides!
    spans += "<span>"+ (i+1) +"</span>";
}
// Finally append our generated buttons:
$nav.append( spans );
// Retrieve the created buttons and do something on click
$nav.find("span").on("click", function(){
   c = $(this).index(); // Set our counter to the clicked SPAN index
   anim();              // Animate. That easy.
});


    // SETUP (fade or slide?)
    $mov.width(w*n);
    if(efx==="fade") $sli.css({position:"absolute", left:0}).fadeOut(0).eq(c).fadeIn(0);

    function populateDescription() {
        $dsc.text( $sli.eq(c).find("img").attr("alt") );
    }

    function anim() {
        c = c<0 ? n-1 : c%n; // loop-back if exceedds
        populateDescription();
        if (efx==="fade") {
            $sli.fadeOut(animationTime).eq(c).stop().fadeIn(animationTime);
        }else if(efx==="slide") {
            $mov.stop().animate({left: -c*w});
        }
    }

    function auto() {
        $btn.add($dsc).add($nav).stop().fadeOut(); // Hide UI
        itv = setInterval(function(){
           ++c;
           anim();
        }, pauseTime);
    }

    function pause() {
    $btn.add($dsc).add($nav).stop().fadeIn(); // Show UI
        return clearInterval( itv );
    }

  $gal.hover(pause, auto);

    $btn.on("click", function(){
        c = this.id==="next" ? ++c : --c;
        anim();
    });


    populateDescription();
    auto();

});
<div id="images_holder">
          <div id="moving_part">
              <div>
                  <a href="You-Belong">
<img src="images/For-Smithfield-Slider-600x232.png" 
srcset="
images/For-Smithfield-Slider-1000x387.png 1000w, 
images/For-Smithfield-Slider-1920x743.png 1920w,
images/For-Smithfield-Slider-2880x1114.png 2880w
" alt=""/>
</a>
              </div>
              <div>
                  <a href="You-Belong">
<img alt="" src="images/You-Belong-Here-Slider1.png"></a>
              </div>
              <div>
                  <img alt="" src="images/Vision-Statement-01.png">
              </div>
          </div>

          <div id="prev">PREV</div>
          <div id="next">NEXT</div>
          <div id="description">Test</div>
          <div id="navigation"></div>
      </div>

这是我正在努力学习的一个活跃项目。我以前从未做过这样的事。任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

也许这对问题1有帮助:Equals heights of thumbnails

为您的图片添加一些自定义类,该代码应该可以将.thumbnail更改为.yourcustomclass。

相关问题