简体   繁体   English

滑块未显示生成的克隆的标题

[英]Slider not showing captions for generated clones

I have this image slider that was build using bxslider which is supposed to show captions when the mouse hovers over the image. 我有一个使用bxslider构建的图像滑块,当鼠标悬停在图像上时,该滑块应该显示标题。 The code works just fine for images that I put in but there is a list of clones generated in the slider for which the captions don't work. 该代码对我放入的图像效果很好,但是在滑块中生成了一个克隆列表,字幕无法使用。 I'm not sure how to get them working. 我不确定如何使他们工作。

           $('.thisOne',this).hover(function() {        
    var title = $(this).attr('title');   
    if (title != undefined && ('' + title).length) {    
        $(this.parentNode).append('<div class="bx-caption"><span>' + title + '</span></div>');                       
    }               
    });  

The broswer generates a copy of the images with a class 'bx-clone' out the list and appends them the list of images such as the one below. 浏览器生成列表为“ bx-clone”的图像的副本,并将其添加到图像列表(例如下面的列表)中。

        <ul class="bxslider1">
          <li><img class="thisOne" img src="images/pic1.jpg" title="Lorem ipsum dsafkjhsdaflkhdsafkj"/></li>
          <li><img class="thisOne" src="images/pic2.jpg"     title="Lorem ipsum dsafkjhsdaflkhdsafkj" /></li>
          <li><img class="thisOne" src="images/pic3.jpg"     title="Lorem ipsum dsafkjhsdaflkhdsafkj" /></li>
          <li><img class="thisOne" src="images/pic4.jpg"     title="Lorem ipsum dslfakjds;lafkjds;lafkjg;oljdfgopi ds ogijugjdfoi" /></li>
          <li><img class="thisOne" src="images/pic5.jpg"     title="Lorem ipsum dslfakjds;lafkjds;lafkjg;oljdfgopi ds ogijugjdfoi" /></li>
          <li><img class="thisOne" src="images/pic7.jpg"     title="Lorem ipsum dslfakjds;lafkjds;lafkjg;oljdfgopi ds ogijugjdfoi" /></li>
        </ul>   

I don't know how to get the cpations to show up on top of the duplicate images. 我不知道如何让cpations显示在重复的图像之上。 If it helps I wrapped my function inside the $(document).ready(); 如果有帮助,我可以将函数包装在$(document).ready();中。 I'm sorry but I don't have a better way of explaining the code. 抱歉,我没有更好的解释代码的方法。

Here is your solution 这是您的解决方案

What I have done is, used a custom attribute called slide and assigned unique slide numbers to the different images. 我所做的是,使用了一个名为slide的自定义属性,并为不同的图像分配了唯一的幻灯片编号。 And on hover try to find the cloned object with the same attribute. 并在悬停时尝试查找具有相同属性的克隆对象。

Also your code kept appending the caption on each hover. 此外,您的代码还会在每个悬停时附加标题。 It's not optimum, so added a check to see if caption is added already. 这不是最佳选择,因此添加了检查以查看是否已添加字幕。

   $('img').hover(function() {        
   var title = $(this).attr('title');   
   var slideNo = this.getAttribute('slide');
   if (title != undefined && ('' + title).length) {    
     var captionAdded = $(this.parentNode).find('.bx-caption');
     if(captionAdded.length == 0){
        var imgObjs = $(this).parents('ul').find('img[slide='+slideNo + ']');
        for(var i=0; i<imgObjs.length; i++){
          $(imgObjs[i].parentNode).append('<div class="bx-caption"><span>' + title + '</span></div>');    
        }
     }
  }               
  });  

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

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