【问题标题】:jquery fade out and on click functionjquery淡出和点击功能
【发布时间】:2014-07-07 15:07:03
【问题描述】:

希望你们一切都好。

所以我的画廊没有使用任何插件

 $(document).ready(function() {
 var index =1;
    var images = ['1.jpg','2.jpg','3.jpg'];
    var caption_text = ["title1","title2","title3"];
    function rotateImage()
    {

       $('#gallery').fadeOut('fast', function() 
      { 
        $(this).attr('src','<?php echo base_url()."assets/highlight/";?>'+ images[index]);
        $('span').text(caption_text[index]);
        $(this).fadeIn('fast', function() 
        { 
          if (index == images.length-1)
          {
            index = 0;
          }
          else
          {
            index++;
          }

        });
      });

    } 
    setInterval (rotateImage, 2500);

$('.thumb').on('click', function() {
        var img = $('<img />', {src    : this.src,
                                'class': 'highlight_img'
                  });
        var imageTitle = $(this).attr("title");      
        $('.highlight').html(img).show();
        $('.highlight').append("<br/><span class='highlight_caption'>"+imageTitle+"</span>");
        setInterval (rotateImage, 5000);  
    });
 });

这是我的html

<div class='col-md-12 highlight'>
    <img id='gallery' src='<?php  echo site_url('assets/highlight/1.jpg');?>' height='300' class='highlight_img'/><br/>
    <span id='highlight_caption' class='highlight_caption'>title1</span>
</div>

<div class='list'>
 <div><img class='thumb' src='<?php  echo site_url('assets/highlight/1.jpg');?>' height='75' title='title1'/></div>
 <div><img class='thumb' src='<?php  echo site_url('assets/highlight/2.jpg');?>' height='75' title='title2'/></div>
 <div><img class='thumb' src='<?php  echo site_url('assets/highlight/3.jpg');?>' height='75' title='title3'/></div>

现在,当页面加载没有问题时,我可以进行图像旋转。此外,当我单击图像缩略图时,#gallery div 也会根据我单击的缩略图更改图像

但是,当我调用缩略图点击功能时,rotateImage() 不再起作用,我需要刷新页面才能让图像再次旋转。

我应该如何编写代码来执行此操作?

谢谢!

编辑:

抱歉不清楚 我的问题是,我放了“setInterval (rotateImage, 5000);”在 click() 函数的 .thumb 内,我知道它正在运行,因为我尝试 console.log,并且脚本确实执行了,但是为什么图像没有改变?

【问题讨论】:

  • 这个问题你不会得到太多答案。没有人会为你编码。他们将帮助您找到错误,因此请将您的问题更改为 how can i avoid this issue 或类似的内容...
  • @Dwza 嗨,我编辑了我的问题。

标签: javascript jquery html css


【解决方案1】:

你可以试试这个吗?

JQUERY

 $(document).ready(function() {
    var index         = 1,
        images        = ['300x300','290x300','280x300'],
        caption_text  = ['title1','title2','title3'],
        timer         = null;

    function rotateImage () {

       $('#gallery').fadeOut('fast', function() { 
       $(this).attr('src','http://www.placehold.it/'+ images[index]);
       $('span').text(caption_text[index]);
       $(this).fadeIn('fast', function() { 
           if (index == images.length-1) {
             index = 0;
           } else {
             index++;
           }
        });
      });
    } 
timer = setInterval (rotateImage, 2500);

$('.thumb').on('click', function() {
    var $this      = $(this),
        img        = $this.attr('src'),
        imageTitle = $this.attr('title');      
    $('.highlight')
        .children('img')
        .attr('src',img)
        .show()
        .end()
            .children('.highlight_caption')
                .text(imageTitle);
        clearInterval(timer);
        setInterval (rotateImage, 5000);  
    });
 });

HTML

<div class='col-md-12 highlight'>
    <img id='gallery' src='http://www.placehold.it/300x300' height='300' class='highlight_img'/>    <br/>
    <span id='highlight_caption' class='highlight_caption'>title1</span>
</div>

<div class='list'>
    <div><img class='thumb' src='http://www.placehold.it/400x400' height='75' title='title1'/>    </div>
 <div><img class='thumb' src='http://www.placehold.it/500x500' height='75' title='title2'/></div>
 <div><img class='thumb' src='http://www.placehold.it/350x350' height='75' title='title3'/></div>

http://jsfiddle.net/DKw8D/6/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 2019-09-08
    相关资源
    最近更新 更多