2012-09-17 203 views

回答

2

http://jsfiddle.net/ARTsinn/MvRdD/890/

$(document).ready(function() { 
    $.getScript("https://raw.github.com/danro/easing-js/master/easing.min.js"); 
    var animateTime = 10, 
     offsetStep = 5, 
     scrollWrapper = $('#wrap'); 

    //event handling for buttons "left", "right" 
    var aktiv; 
    $('.bttL, .bttR').mousedown(function(e) { 
     if (e.target.className === 'bttR') { 
      aktiv = window.setInterval(function() { 
       scrollWrapper.animate({ 
        scrollLeft: '+=' + 20 
       }, { 
        duration: 600, 
        queue: false, 
        easing: 'easeOutCirc' 
       }); 
      }, 10); 
     } else if (e.target.className === 'bttL') { 
      aktiv = window.setInterval(function() { 

       scrollWrapper.animate({ 
        scrollLeft: '-=' + 20 
       }, { 
        duration: 1200, 
        queue: false, 
        easing: 'easeOutCirc' 
       }); 
      }, 10); 

     } 
    }).mouseup(function() { 
     window.clearInterval(aktiv); 
    }); 


    scrollWrapper.mousedown(function(event) { 
     $(this).data('down', true).data('x', event.clientX).data('scrollLeft', this.scrollLeft); 
     return false; 
    }).mouseup(function(event) { 
     $(this).data('down', false); 
    }).mousemove(function(event) { 
     if ($(this).data('down')) { 
      $(this).stop(false, true).animate({ 
       scrollLeft: $(this).data('scrollLeft') + ($(this).data('x') - event.clientX) * 2 
      }, { 
       duration: 600, 
       queue: false, 
       easing: 'easeOutCirc' 
      }); 
     } 
    }).mousewheel(function(event, delta) { 
     $(this).stop(false, true).animate({ 
      scrollLeft: '-=' + delta * 60 
     }, { 
      duration: 400, 
      queue: false, 
      easing: 'easeOutCirc' 
     }); 
     event.preventDefault(); 
    }).css({ 
     'overflow': 'hidden', 
     'cursor': '-moz-grab' 
    }); 
}); 
+0

没办法啊,这是真棒!它在拖动和鼠标滚轮上确实轻松,但不会按下按钮? – legoman

+0

这就是你现在学习JavaScript的一部分:)如果你在超过30分钟内失败了,只需重新写一遍,我也会为你完成这部分: - * – yckart

+0

明天就要到了,但是忍耐着我! – legoman