Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

smooth scroll mouse wheel javascript

function init(){
	new SmoothScroll(document,120,12)
}

function SmoothScroll(target, speed, smooth) {
	if (target === document)
		target = (document.scrollingElement 
              || document.documentElement 
              || document.body.parentNode 
              || document.body) // cross browser support for document scrolling
      
	var moving = false
	var pos = target.scrollTop
  var frame = target === document.body 
              && document.documentElement 
              ? document.documentElement 
              : target // safari is the new IE
  
	target.addEventListener('mousewheel', scrolled, { passive: false })
	target.addEventListener('DOMMouseScroll', scrolled, { passive: false })

	function scrolled(e) {
		e.preventDefault(); // disable default scrolling

		var delta = normalizeWheelDelta(e)

		pos += -delta * speed
		pos = Math.max(0, Math.min(pos, target.scrollHeight - frame.clientHeight)) // limit scrolling

		if (!moving) update()
	}

	function normalizeWheelDelta(e){
		if(e.detail){
			if(e.wheelDelta)
				return e.wheelDelta/e.detail/40 * (e.detail>0 ? 1 : -1) // Opera
			else
				return -e.detail/3 // Firefox
		}else
			return e.wheelDelta/120 // IE,Safari,Chrome
	}

	function update() {
		moving = true
    
		var delta = (pos - target.scrollTop) / smooth
    
		target.scrollTop += delta
    
		if (Math.abs(delta) > 0.5)
			requestFrame(update)
		else
			moving = false
	}

	var requestFrame = function() { // requestAnimationFrame cross browser
		return (
			window.requestAnimationFrame ||
			window.webkitRequestAnimationFrame ||
			window.mozRequestAnimationFrame ||
			window.oRequestAnimationFrame ||
			window.msRequestAnimationFrame ||
			function(func) {
				window.setTimeout(func, 1000 / 50);
			}
		);
	}()
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: foreach jas 
Javascript :: not in array js 
Javascript :: toast message angular 
Javascript :: js find index in list 
Javascript :: set node_env 
Javascript :: send serialized form data jquery 
Javascript :: uncaught typeerror is not a function javascript 
Javascript :: math.floor js 
Javascript :: how to check localstorage not set 
Javascript :: how to hide title bar react-navigation 
Javascript :: javascript test if string starts with alphabet 
Javascript :: javas script add list 
Javascript :: hex to rgb function 
Javascript :: how to remove whitespace only from first position of string js 
Javascript :: javascript format date time 
Javascript :: queryselector get each element 
Javascript :: change px string to number 
Javascript :: jquery display modal bs4 
Javascript :: jquery insert after next element 
Javascript :: axios Request body larger than maxBodyLength limit 
Javascript :: javascript response redirect 
Javascript :: save canvas as image from website 
Javascript :: js fetch get params 
Javascript :: javascript next month from date 
Javascript :: check css property jquery 
Javascript :: reactjs radio button onchange 
Javascript :: how to read xml element in xml2js 
Javascript :: ajax open new tab with post 
Javascript :: Find a vowel at the begining and end with regular expression 
Javascript :: react native header 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =