2017-05-18 72 views
0

我一直在試圖弄清爲什麼我的smoothscroll功能在Firefox中不起作用?它在Chrome,Microsoft Edge等其他瀏覽器中運行良好。請有人幫助我。請檢查此筆:https://codepen.io/anon/pen/oWPaNx鼠標滾輪不能在Firefox中工作

function smoothScroll(time, distance) { 
    var $window = $(window); 
    var scrollTime = time; 
    var scrollDistance = distance; 

    $window.on("wheel mousewheel DOMMouseScroll", function (event) { 

     event.preventDefault(); 
     var delta = event.originalEvent.wheelDelta/80 || -event.originalEvent.detail/3; 

     var scrollTop = $window.scrollTop(); 
     var finalScroll = scrollTop - parseInt(delta * scrollDistance); 

     TweenMax.to($window, scrollTime, { 
      scrollTo: { 
       y: finalScroll, 
       autoKill: true 
      }, 
      ease: Power4.easeOut, 
      overwrite: 5 
     }); 

    }); 
} 
+0

你的代碼工作。你忘了添加這個插件:https://cdnjs.cloudflare.com/ajax/libs/gsap/1.19.1/plugins/ScrollToPlugin.min.js – Phil

+0

@Phil謝謝你,但它仍然不能在Firefox中工作。檢查更新的筆請https://codepen.io/anon/pen/oWPaNx – Enthusiast

回答

1

Firefox中的滾動行爲是有點怪我。

這是一個討論中,我已經找到mousewheel event is not triggering in firefox browser

function smoothScroll(time, distance) { 

    // added this 
    var isFirefox = (/Firefox/i.test(navigator.userAgent)); 
    var scrollEvent = isFirefox ? "DOMMouseScroll" : "mousewheel" 

    var $window = $(window); 
    var scrollTime = time; 
    var scrollDistance = distance; 

    // insert scrollEvent var 
    $window.on(scrollEvent, function(event) { 

    // added this (check if firefox) 
    if (!isFirefox) { 
     event.preventDefault(); 
    } 

    var delta = event.originalEvent.wheelDelta/80 || -event.originalEvent.detail/3; 

    var scrollTop = $window.scrollTop(); 
    var finalScroll = scrollTop - parseInt(delta * scrollDistance); 

    // changed window 
    TweenMax.to(window, scrollTime, { 
     scrollTo: { 
     y: finalScroll, 
     autoKill: true 
     }, 
     ease: Power4.easeOut, 
     overwrite: 5 
    }); 

    }); 
} 

smoothScroll(1.1, 110); 
+0

感謝您的答案。這確實有效,但它很漂亮,但它確實回答了這個問題。 – Enthusiast

相關問題