2014-10-10 37 views
0

我在滑塊上的工作,這裏是它的代碼:位置()被視爲一個功能

$(document).ready(function() { 
      // MAKE SLIDER WIDTH EQUAL TO ALL SLIDES WIDTH 
    var totalWidth = 0; 
    $('.slide').each(function() { 
     totalWidth = totalWidth + $(this).outerWidth(true); 
    }); 
    var maxScrollPosition = totalWidth - $(".slider_wrap").outerWidth(); 

    function slideMove($targetSlide) { 
     if ($targetSlide.length) { 
      var newPosition = $targetSlide.position().left; 
      if (newPosition <= maxScrollPosition) { 
       $targetSlide.addClass('slide--active'); 
       $targetSlide.siblings().removeClass("slide--active"); 
       $(".slider").animate({ 
        left : - newPosition 
       }); 
      } 
      else { 
       $(".slider").animate({ 
        left : - maxScrollPosition 
       }); 
      }; 
     }; 
    }; 
    $(".slide").width(totalWidth); 

    $(".slide:first").addClass("slide--active"); 
    $(".next_post").click(function(){ 
     var $targetItem = $(".slide--active").prev(); 
     slideMove('.slide'); 
    }); 
    $(".prev_post").click(function(){ 
     var $targetItem = $(".slide--active").next(); 
     slideMove('.slide'); 
    }); 
    }); 

雖然我希望這個工作,我得到一個錯誤,指出:類型錯誤: $ targetSlide.position未定義。從函數slideMove中的if語句開始。爲什麼這不起作用?如果這是一個明顯的答案,我仍然在學習jQuery,所以很抱歉。

回答

1

改變這種

slideMove('.slide'); 

這個

slideMove($('.slide')); 

在你的方法slideMove你期待一個jQuery DOM元素在那裏,你給它只是一個字符串這是該特定DOM元素的類名稱

0

YO!將其更改爲這個

... 

$('.next_post').click(function(){ 
    var $targetItem = $('.slide--active').prev(); 
    slideMove($('.slide')); 
}); 

$('.prev_post').click(function(){ 
    var $targetItem = $('.slide--active').next(); 
    slideMove($('.slide')); 
}); 

... 
+0

愛喲,dawg – Fyxerz 2014-10-10 22:50:08