2012-05-31 2 views
-4

안녕하세요. 먼저 코딩이나 기타 관련있는 간단한 질문이 없습니다. 슬라이드를 자동 시작하기 위해이 코드를 간단하게 말할 수있는 방법이 있습니까? 현재 순간에 클릭하면 이미지가 변경됩니다.이 슬라이드를 자동 시작하는 방법

<script type="text/javascript"> 
//<![CDATA[ 
    /* 
    the images preload plugin 
    */ 
    (function($) { 
     $.fn.preload = function(options) { 
      var opts = $.extend({}, $.fn.preload.defaults, options), 
       o  = $.meta ? $.extend({}, opts, this.data()) : opts; 
      var c  = this.length, 
       l  = 0; 
      return this.each(function() { 
       var $i = $(this); 
       $('<img/>').load(function(i){ 
        ++l; 
        if(l == c) o.onComplete(); 
       }).attr('src',$i.attr('src')); 
      }); 
     }; 
     $.fn.preload.defaults = { 
      onComplete : function(){return false;} 
     }; 
    })(jQuery); 
//]]> 
</script><script type="text/javascript"> 
//<![CDATA[ 
    $(function() { 
     var $tf_bg    = $('#tf_bg'), 
      $tf_bg_images  = $tf_bg.find('img'), 
      $tf_bg_img   = $tf_bg_images.eq(0), 
      $tf_thumbs   = $('#tf_thumbs'), 
      total    = $tf_bg_images.length, 
      current    = 0, 
      $tf_content_wrapper = $('#tf_content_wrapper'), 
      $tf_next   = $('#tf_next'), 
      $tf_prev   = $('#tf_prev'), 
      $tf_loading   = $('#tf_loading'); 

     //preload the images     
     $tf_bg_images.preload({ 
      onComplete : function(){ 
       $tf_loading.hide(); 
       init(); 
      } 
     }); 

     //shows the first image and initializes events 
     function init(){ 
      //get dimentions for the image, based on the windows size 
      var dim = getImageDim($tf_bg_img); 
      //set the returned values and show the image 
      $tf_bg_img.css({ 
       width : dim.width, 
       height : dim.height, 
       left : dim.left, 
       top  : dim.top 
      }).fadeIn(); 

      //resizing the window resizes the $tf_bg_img 
      $(window).bind('resize',function(){ 
       var dim = getImageDim($tf_bg_img); 
       $tf_bg_img.css({ 
        width : dim.width, 
        height : dim.height, 
        left : dim.left, 
        top  : dim.top 
       }); 
      }); 

      //expand and fit the image to the screen 
      $('#tf_zoom').live('click', 
       function(){ 
        if($tf_bg_img.is(':animated')) 
         return false; 

        var $this = $(this); 
        if($this.hasClass('tf_zoom')){ 
         resize($tf_bg_img); 
         $this.addClass('tf_fullscreen') 
          .removeClass('tf_zoom'); 
        } 
        else{ 
         var dim = getImageDim($tf_bg_img); 
         $tf_bg_img.animate({ 
          width : dim.width, 
          height : dim.height, 
          top  : dim.top, 
          left : dim.left 
         },350); 
         $this.addClass('tf_zoom') 
          .removeClass('tf_fullscreen'); 
        } 
       } 
      ); 

      //click the arrow down, scrolls down 
      $tf_next.bind('click',function(){ 
       if($tf_bg_img.is(':animated')) 
        return false; 
        scroll('tb'); 
      }); 

      //click the arrow up, scrolls up 
      $tf_prev.bind('click',function(){ 
       if($tf_bg_img.is(':animated')) 
        return false; 
       scroll('bt'); 
      }); 

      //mousewheel events - down/up button trigger the scroll down/up 
      $(document).mousewheel(function(e, delta) { 
       if($tf_bg_img.is(':animated')) 
        return false; 

       if(delta > 0) 
        scroll('bt'); 
       else 
        scroll('tb'); 
       return false; 
      }); 

      //key events - down/up button trigger the scroll down/up 
      $(document).keydown(function(e){ 
       if($tf_bg_img.is(':animated')) 
        return false; 

       switch(e.which){ 
        case 38:  
         scroll('bt'); 
         break; 
        case 40:  
         scroll('tb'); 
         break; 
       } 
      }); 
     } 

     //show next/prev image 
     function scroll(dir){ 
      //if dir is "tb" (top -> bottom) increment current, 
      //else if "bt" decrement it 
      current = (dir == 'tb')?current + 1:current - 1; 

      //we want a circular slideshow, 
      //so we need to check the limits of current 
      if(current == total) current = 0; 
      else if(current < 0) current = total - 1; 

      //flip the thumb 
      $tf_thumbs.flip({ 
       direction : dir, 
       speed  : 400, 
       onBefore : function(){ 
        //the new thumb is set here 
        var content = '<span id="tf_zoom" class="tf_zoom"><\/span>'; 
        content  +='<img src="' +   $tf_bg_images.eq(current).attr('longdesc') + '" alt="Thumb' + (current+1) + '"/>'; 
        $tf_thumbs.html(content); 
       } 
      }); 

      //we get the next image 
      var $tf_bg_img_next = $tf_bg_images.eq(current), 
       //its dimentions 
       dim    = getImageDim($tf_bg_img_next), 
       //the top should be one that makes the image out of the viewport 
       //the image should be positioned up or down depending on the direction 
        top = (dir == 'tb')?$(window).height() + 'px':-parseFloat(dim.height,10) + 'px'; 

      //set the returned values and show the next image 
       $tf_bg_img_next.css({ 
        width : dim.width, 
        height : dim.height, 
        left : dim.left, 
        top  : top 
       }).show(); 

      //now slide it to the viewport 
       $tf_bg_img_next.stop().animate({ 
        top  : dim.top 
       },700); 

      //we want the old image to slide in the same direction, out of the viewport 
       var slideTo = (dir == 'tb')?-$tf_bg_img.height() + 'px':$(window).height() + 'px'; 
       $tf_bg_img.stop().animate({ 
        top  : slideTo 
       },700,function(){ 
       //hide it 
        $(this).hide(); 
       //the $tf_bg_img is now the shown image 
        $tf_bg_img = $tf_bg_img_next; 
       //show the description for the new image 
        $tf_content_wrapper.children() 
             .eq(current) 
             .show(); 
      }); 
       //hide the current description 
        $tf_content_wrapper.children(':visible') 
             .hide() 

      } 

      //animate the image to fit in the viewport 
      function resize($img){ 
       var w_w = $(window).width(), 
        w_h = $(window).height(), 
        i_w = $img.width(), 
        i_h = $img.height(), 
        r_i = i_h/i_w, 
        new_w,new_h; 

       if(i_w > i_h){ 
        new_w = w_w; 
        new_h = w_w * r_i; 

        if(new_h > w_h){ 
         new_h = w_h; 
         new_w = w_h/r_i; 
        } 
       } 
       else{ 
        new_h = w_w * r_i; 
        new_w = w_w; 
       } 

       $img.animate({ 
        width : new_w + 'px', 
        height : new_h + 'px', 
        top  : '0px', 
        left : '0px' 
       },350); 
      } 

      //get dimentions of the image, 
      //in order to make it full size and centered 
      function getImageDim($img){ 
       var w_w = $(window).width(), 
        w_h = $(window).height(), 
        r_w = w_h/w_w, 
        i_w = $img.width(), 
        i_h = $img.height(), 
        r_i = i_h/i_w, 
        new_w,new_h, 
        new_left,new_top; 

       if(r_w > r_i){ 
        new_h = w_h; 
        new_w = w_h/r_i; 
       } 
       else{ 
        new_h = w_w * r_i; 
        new_w = w_w; 
       } 


       return { 
        width : new_w + 'px', 
        height : new_h + 'px', 
        left : (w_w - new_w)/2 + 'px', 
        top  : (w_h - new_h)/2 + 'px' 
       }; 
       } 
     }); 
//]]> 
</script> 
+3

관련 코드 만 표시하십시오. –

답변

0

쉬운 방법은 setTimeout()을 설정하고이 "마우스 클릭"을 처리하는 기능을 실행하는 것입니다.

페이지가이 라인을 발사 할 수 로딩이 완료되면 : 그 2 초 후에 사진을 변경할 수 있습니다

var timer = setTimeout(function(){ 
    scroll(tb); 
}, 2000); 

합니다.

//Fire this after page has finished loading 
var timer = setTimeout(function(){ 
    autoChange("tb", 2000); 
}, 2000); 

//Put this somewhere else 
function autoChange(dir, timeout){ 
    scroll(dir); 
    var timer = setTimeout(function(){ 
     autoChange("tb", 2000); 
    }, timeout); 
} 

즉, 사진의 변화를 2 초마다 만들어 줄게 :

당신은 당신이 이런 식으로 뭔가를 할 수 계속합니다. 사용자가 클릭하여 그림을 변경하면 제한 시간을 5 초로 변경하여 2 초마다 자동 스크롤을 시작할 수 있습니다.

setTimeout()W3Schools에서 읽으십시오.

+0

이 메소드는'Timeout'이 아니라'setTimeout'입니다. 또한 문자열이 아닌'setTimeout' 함수를 전달합니다 (문자열을 전달할 수 있지만'eval'ed가 발생합니다.). 'var timer = Timeout (autoChange, 2000); 또는'var timer = Timeout (function() {autoChange();});'두 번째를 제안하면 매개 변수를 전달할 수 있습니다. autoChange '로 변경하십시오. 'var timer = Timeout (function() {autoChange (tb, 2000);}, 2000); –

+0

다음은'setTimeout'을위한 몇 가지 (더 좋은) 문서들입니다 : https://developer.mozilla.org/en/DOM/ window.setTimeout –

+0

'setTimeout'은 함수를 한 번만 실행합니다. 우리는 여기서'setInterval'을 찾고 있습니다. – lbstr

2

인가가 어떤 : 현재 인덱스 페이지는 하나 개의 이미지를 가지고, 내가 원하는 것은 여기에 다음 하나 을 보시려면 클릭 할 필요없이 몇하지만를 추가하는 것은 내 인덱스의 코드입니다 간단한 ...

없음 :

setTimeout() 기능을 살펴보십시오.하지 어쩌면이 도움이 될 수 :

0

스크롤 당신이 볼 때까지이 코드 아래 MY ANSWER!!!!!!!!

편집 : 나는 몇 가지 업데이트되면서 코드를 붙여 넣은 . 다시 해봐.

<script type="text/javascript"> 
//<![CDATA[ 
    /* 
    the images preload plugin 
    */ 
    (function($) { 
     $.fn.preload = function(options) { 
      var opts = $.extend({}, $.fn.preload.defaults, options), 
       o  = $.meta ? $.extend({}, opts, this.data()) : opts; 
      var c  = this.length, 
       l  = 0; 
      return this.each(function() { 
       var $i = $(this); 
       $('<img/>').load(function(i){ 
        ++l; 
        if(l == c) o.onComplete(); 
       }).attr('src',$i.attr('src')); 
      }); 
     }; 
     $.fn.preload.defaults = { 
      onComplete : function(){return false;} 
     }; 
    })(jQuery); 
//]]> 
</script><script type="text/javascript"> 
//<![CDATA[ 
    $(function() { 
     var $tf_bg    = $('#tf_bg'), 
      $tf_bg_images  = $tf_bg.find('img'), 
      $tf_bg_img   = $tf_bg_images.eq(0), 
      $tf_thumbs   = $('#tf_thumbs'), 
      total    = $tf_bg_images.length, 
      current    = 0, 
      $tf_content_wrapper = $('#tf_content_wrapper'), 
      $tf_next   = $('#tf_next'), 
      $tf_prev   = $('#tf_prev'), 
      $tf_loading   = $('#tf_loading'); 


     //preload the images     
     $tf_bg_images.preload({ 
      onComplete : function(){ 
       $tf_loading.hide(); 
       init(); 
      } 
     }); 


     //shows the first image and initializes events 
     function init(){ 
      //get dimentions for the image, based on the windows size 
      var dim = getImageDim($tf_bg_img); 
      //set the returned values and show the image 
      $tf_bg_img.css({ 
       width : dim.width, 
       height : dim.height, 
       left : dim.left, 
       top  : dim.top 
      }).fadeIn(); 

      //resizing the window resizes the $tf_bg_img 
      $(window).bind('resize',function(){ 
       var dim = getImageDim($tf_bg_img); 
       $tf_bg_img.css({ 
        width : dim.width, 
        height : dim.height, 
        left : dim.left, 
        top  : dim.top 
       }); 
      }); 

      // MY ANSWER!!!!!!!!!! 
      // Click next every 5000 ms (5 sec). 
      setInterval(function(){ 
       $tf_next.click(); 
      }, 5000); 
     } 

     //expand and fit the image to the screen 
     $('#tf_zoom').live('click', function(){ 
      if($tf_bg_img.is(':animated')) 
       return false; 

      var $this = $(this); 
      if($this.hasClass('tf_zoom')){ 
       resize($tf_bg_img); 
       $this.addClass('tf_fullscreen') 
        .removeClass('tf_zoom'); 
      } 
      else{ 
       var dim = getImageDim($tf_bg_img); 
       $tf_bg_img.animate({ 
        width : dim.width, 
        height : dim.height, 
        top  : dim.top, 
        left : dim.left 
       },350); 
       $this.addClass('tf_zoom') 
        .removeClass('tf_fullscreen'); 
      } 
     }); 

     //click the arrow down, scrolls down 
     $tf_next.bind('click',function(){ 
      if($tf_bg_img.is(':animated')) 
       return false; 
      scroll('tb'); 
     }); 

     //click the arrow up, scrolls up 
     $tf_prev.bind('click',function(){ 
      if($tf_bg_img.is(':animated')) 
       return false; 
      scroll('bt'); 
     }); 

     //mousewheel events - down/up button trigger the scroll down/up 
     $(document).mousewheel(function(e, delta) { 
      if($tf_bg_img.is(':animated')) 
       return false; 

      if(delta > 0) 
       scroll('bt'); 
      else 
       scroll('tb'); 
      return false; 
     }); 

     //key events - down/up button trigger the scroll down/up 
     $(document).keydown(function(e){ 
      if($tf_bg_img.is(':animated')) 
       return false; 

      switch(e.which){ 
       case 38:  
        scroll('bt'); 
        break; 

       case 40:  
        scroll('tb'); 
        break; 
      } 
     }); 


      //show next/prev image 
     function scroll(dir){ 
      //if dir is "tb" (top -> bottom) increment current, 
      //else if "bt" decrement it 
      current = (dir == 'tb')?current + 1:current - 1; 

      //we want a circular slideshow, 
      //so we need to check the limits of current 
      if(current == total) current = 0; 
      else if(current < 0) current = total - 1; 

      //flip the thumb 
      $tf_thumbs.flip({ 
       direction : dir, 
       speed  : 400, 
       onBefore : function(){ 
        //the new thumb is set here 
        var content = '<span id="tf_zoom" class="tf_zoom"><\/span>'; 
        content  +='<img src="' + $tf_bg_images.eq(current).attr('longdesc') + '" alt="Thumb' + (current+1) + '"/>'; 
        $tf_thumbs.html(content); 
       } 
      }); 

      //we get the next image 
      var $tf_bg_img_next = $tf_bg_images.eq(current), 
       //its dimentions 
       dim    = getImageDim($tf_bg_img_next), 
       //the top should be one that makes the image out of the viewport 
       //the image should be positioned up or down depending on the direction 
       top = (dir == 'tb')?$(window).height() + 'px':-parseFloat(dim.height,10) + 'px'; 

      //set the returned values and show the next image 
      $tf_bg_img_next.css({ 
       width : dim.width, 
       height : dim.height, 
       left : dim.left, 
       top  : top 
      }).show(); 

      //now slide it to the viewport 
      $tf_bg_img_next.stop().animate({ 
       top  : dim.top 
      },700); 

      //we want the old image to slide in the same direction, out of the viewport 
      var slideTo = (dir == 'tb')?-$tf_bg_img.height() + 'px':$(window).height() + 'px'; 
      $tf_bg_img.stop().animate({ 
        top  : slideTo 
       },700,function(){ 
       //hide it 
        $(this).hide(); 
       //the $tf_bg_img is now the shown image 
        $tf_bg_img = $tf_bg_img_next; 
       //show the description for the new image 
        $tf_content_wrapper.children() 
             .eq(current) 
             .show(); 
      }); 
      //hide the current description 
       $tf_content_wrapper.children(':visible') 
            .hide() 

     } 

     //animate the image to fit in the viewport 
     function resize($img){ 
      var w_w = $(window).width(), 
       w_h = $(window).height(), 
       i_w = $img.width(), 
       i_h = $img.height(), 
       r_i = i_h/i_w, 
       new_w,new_h; 

      if(i_w > i_h){ 
       new_w = w_w; 
       new_h = w_w * r_i; 

       if(new_h > w_h){ 
        new_h = w_h; 
        new_w = w_h/r_i; 
       } 
      } 
      else{ 
       new_h = w_w * r_i; 
       new_w = w_w; 
      } 

      $img.animate({ 
       width : new_w + 'px', 
       height : new_h + 'px', 
       top  : '0px', 
       left : '0px' 
      },350); 
     } 

     //get dimentions of the image, 
     //in order to make it full size and centered 
     function getImageDim($img){ 
      var w_w = $(window).width(), 
       w_h = $(window).height(), 
       r_w = w_h/w_w, 
       i_w = $img.width(), 
       i_h = $img.height(), 
       r_i = i_h/i_w, 
       new_w,new_h, 
       new_left,new_top; 

      if(r_w > r_i){ 
       new_h = w_h; 
       new_w = w_h/r_i; 
      } 
      else{ 
       new_h = w_w * r_i; 
       new_w = w_w; 
      } 


      return { 
       width : new_w + 'px', 
       height : new_h + 'px', 
       left : (w_w - new_w)/2 + 'px', 
       top  : (w_h - new_h)/2 + 'px' 
      }; 
     } 
    });​ 
//]]> 
</script> 
+0

질문자가 제안 된 코드를 어디에 배치할지 알 수 없으므로 모든 코드가 포함되어 있습니다. OP의 첫 번째 줄에는 코드에 대해 알지 못하기 때문에 여기서 수용하겠습니다. – lbstr

+0

@lbstr에 감사드립니다. 답변을 찾았지만 작동하지 않습니다. – lchales

+0

방금 ​​업로드했습니다. http://www.luischales.com – lchales

0

는이 같은 setInterval과 함께 다음 화살표를 클릭 이벤트를 가로 챌 수 있습니다

setInterval(function(){ 
    $tf_next.trigger('click'); 
}, 2500); // Change 2500 to whatever miliseconds delay you want 

행운을 빕니다!

관련 문제