简体   繁体   English

另一个图像滑块JQuery问题

[英]Yet Another Image Slider JQuery Issue

I have trying for some time. 我已经尝试了一段时间。 And I gave a look at the image slider questions already. 我已经看过图像滑块问题了。 But I would really appreciate if I get some specific help here. 但是,如果我在这里得到一些具体帮助,我将不胜感激。 Please tell me where the following code went wrong. 请告诉我以下代码在哪里出错。

Here's my jQuery code: 这是我的jQuery代码:

function slideMe {
  $('.imgcontainer').animate({"margin-left":"-= 100%"}, 2000, function () {
   if($('.imgcontainer').css("margin-left") == (-300%)) {
     $('.imgcontainer').css("margin-left", "0px");
   }
   slideMe();
  });
}
window.onload = slideMe();

And here's the script on HTML page: 这是HTML页面上的脚本:

<div class="fitimage">
    <div class="imgcontainer">
     <img src="img/copywrtng.jpg" class="headerimage" alt="copywriting" />
     <img src="img/copywrtng1.jpg" class="headerimage" alt="copywriting" />
     <img src="img/copywrtng2.jpg" class="headerimage" alt="copywriting" />
    </div>
</div>

And here's what I have on CSS: 这是我在CSS上的功能:

div.fitimage {
    width:100%;
    height:93vh;
    overflow: hidden;
}
img.headerimage {
    padding: 0;
    margin:0 auto;
    width:100%;
  /*max-height:100%;
    max-width:100%;*/
    height:93vh;
}

My logic is that as each image takes up 100% width, as the ".imgcontainer" div slides left with margin-left: -100%, each image should come up one by one from the right, until it reaches the last one when it reverts to the first image again. 我的逻辑是,当每张图片占用100%的宽度时,随着“ .imgcontainer” div向左滑动,边距为-100%,每张图片应从右开始逐一排列,直到到达最后一张再次恢复为第一张图像。

It's not working! 没用!

Please help. 请帮忙。

Your "main" issue is in this line: 您的“主要”问题在以下行中:

$('.imgcontainer').css("margin-left") == (-300 % )
                                       // ^^^^^^^^

beside you can only use string format "-300%" , .css("margin-left") will give you a px value which you cannot compare with a non-computed % . 在旁边,您只能使用字符串格式"-300%" 。. css .css("margin-left") 将为您提供一个px值 ,您无法将其与未计算的%进行比较。

Another issue is that the slides set at 100% - will be the width of their container, which might be actually greater (300% for 3 slides). 另一个问题是将幻灯片设置为100%-将是其容器的宽度,实际上可能更大(3张幻灯片为300%)。

Solution: 解:

Instead I'd suggest you to use a c counter that goes like 0,1,2, 0,1... etc . 相反,我建议您使用像0,1,2, 0,1... etcc计数器。
Than calculate the overflow-wrapper width and animate scrollLeft: width * counter (instead of -marginLeft ). 比计算溢出包装器的宽度和动画scrollLeft: width * counter (而不是-marginLeft )。

Use display:flex on the parent and min-width:100%; 在父级上使用display:flexmin-width:100%; on the child DIV elements (than place your images inside those DIVs instead!) 在子DIV元素上(而不是将图像放在这些DIV中!)

 $(".imgcontainer").each(function(){ // Now you can have multiple independent sliders! var $gal = $(this), $slides = $gal.children(), // get the slides tot = $slides.length, // number of slides c = 0; // the counter we mentioned (function anim() { // internal animation callback function var w = $gal.width(); c = ++c % tot; // increment or loop-back counter to 0 $gal.delay(2000).animate({scrollLeft: w*c }, 1000, anim); // anim callback ! }()); // << START! }); 
 /*QuickReset*/ *{margin:0;box-sizing:border-box;font:14px/1.4 sans-serif;} html,body{height:100%;} .imgcontainer{ position:relative; height: 93%; /* or whatever you see fit */ display: flex; overflow: hidden; } .imgcontainer > div { min-width: 100%; background: none 50% 50% / cover; /* Set background-image inline (see HTML) */ /* Or use inner images like you did */ } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="imgcontainer"> <div style="background-image:url(http://placehold.it/500x340/0bf);"> Slide 1 </div> <div style="background-image:url(http://placehold.it/500x340/f0b);"> Slide 2 </div> <div style="background-image:url(http://placehold.it/500x340/b0f);"> Slide 3 </div> </div> 

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

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