How are you? My Question:
How can I control or specify the way a document scrolls to the position of desire described by either the mouse scrollwheel, an
var $window = $(window),
scrollDistance = 300,
scrollSpeed = 500,
scrollEffect = 'swing',
scrollAmount = 1,
smoothScroll;
if (! ('ontouchstart' in document.documentElement) && ! $('body').hasClass('modal-open')) {
$window.on("mousewheel DOMMouseScroll", function (event) {
event.preventDefault();
if (smoothScroll) {
// Start scrolling while waiting for final scoll amount (user stopped wheeling)
if (scrollAmount == 1) {
var delta = event.originalEvent.wheelDelta / 120 || -event.originalEvent.detail / 3;
var finalScroll = $window.scrollTop() - parseInt(delta * (scrollDistance * scrollAmount));
$('html, body').stop().animate({ scrollTop: finalScroll }, scrollSpeed, scrollEffect);
}
// Increase scroll amount
scrollAmount++;
// Clear current timeout
clearTimeout(smoothScroll);
}
// Set animated scroll
smoothScroll = setTimeout(function() {
var delta = event.originalEvent.wheelDelta / 120 || -event.originalEvent.detail / 3;
var scrollTop = $window.scrollTop();
var finalScroll = scrollTop - parseInt(delta * (scrollDistance * scrollAmount));
// Reset scroll amoount
scrollAmount = 1;
$('html, body').stop().animate({ scrollTop: finalScroll },
(scrollSpeed*scrollAmount),
scrollEffect
);
// Clear timeout holder
smoothScroll = null;
}, 100);
});
}
You want to give this one a try
https://github.com/galambalazs/smoothscroll-for-websites
it has nice settings to adjust the animation and seems to be well maintained.
// Scroll Variables (tweakable)
var defaultOptions = {
// Scrolling Core
frameRate : 100, // [Hz]
animationTime : 1200, // [ms]
stepSize : 80, // [px]
// Pulse (less tweakable)
// ratio of "tail" to "acceleration"
pulseAlgorithm : true,
pulseScale : 4,
pulseNormalize : 1,
// Acceleration
accelerationDelta : 50, // 50
accelerationMax : 3, // 3
// Keyboard Settings
keyboardSupport : true, // option
arrowScroll : 50, // [px]
// Other
touchpadSupport : false, // ignore touchpad by default
fixedBackground : true,
excluded : ''
};
What I wanted to do was to simulate browser's smooth scrolling on browsers that don't support it natively, has it turned off by default or has bad implementation of it.
Thanks to lumbric's answer I've came up with this solution:
$(function () {
var top = 0,
step = 55,
viewport = $(window).height(),
body = $.browser.webkit ? $('body') : $('html'),
wheel = false;
$('body').mousewheel(function(event, delta) {
wheel = true;
if (delta < 0) {
top = (top+viewport) >= $(document).height() ? top : top+=step;
body.stop().animate({scrollTop: top}, 400, function () {
wheel = false;
});
} else {
top = top <= 0 ? 0 : top-=step;
body.stop().animate({scrollTop: top}, 400, function () {
wheel = false;
});
}
return false;
});
$(window).on('resize', function (e) {
viewport = $(this).height();
});
$(window).on('scroll', function (e) {
if (!wheel)
top = $(this).scrollTop();
});
});
Put some content on your page long enough to have scrollbars. Then use your mouse wheel. It works on every browser. I've used jQuery-1.7.2 and the mousescroll plugin mentioned in the lumbric's post.
The step var means how many pixels to scroll on every mouse wheel event. ~55 pixels is what I've found to be the default value on most systems.
Also you may want to change the speed for the animation, other than that the rest of the code logic is needed to get the things work properly.
EDIT: Note that I have extracted the above functionality into a convenience jquery plugin.
my solution with Mobile effect
<div id="parent">
<div id="child">aaa aaaaaaaaa aaaaaaaa aaaaaaaaaaaa abbbbbbbbbbbbbbbbb bbbbbbbbbbbbcc ccccccc ccccccccccc cccccccd ffffdffffdd ffffd ffffffffd ffffffffd ffffdffffffffd aaaaaaaaaaaa aaaaaaaa aaaaaaaaaaaa abbbbbbbbbbbbbbbbb bbbbbbbbbbbbcc ccccccc ccccccccccc cccccccd ffffdffffdd ffffd ffffffffd ffffffffd ffffdffffffffd aaaaaaaaaaaa aaaaaaaa aaaaaaaaaaaa abbbbbbbbbbbbbbbbb bbbbbbbbbbbbcc ccccccc ccccccccccc cccccccd ffffdffffdd ffffd ffffffffd ffffffffd ffffdffffffffd aaaaaaaaaaaa aaaaaaaa aaaaaaaaaaaa abbbbbbbbbbbbbbbbb bbbbbbbbbbbbcc ccccccc ccccccccccc cccccccd ffffdffffdd ffffd ffffffffd ffffffffd ffffdffffffffd aaaaaaaaaaaa aaaaaaaa aaaaaaaaaaaa abbbbbbbbbbbbbbbbb bbbbbbbbbbbbcc ccccccc ccccccccccc cccccccd ffffdffffdd ffffd ffffffffd ffffffffd ffffdffffffffd aaaaaaaaaaaa aaaaaaaa aaaaaaaaaaaa abbbbbbbbbbbbbbbbb bbbbbbbbbbbbcc ccccccc ccccccccccc cccccccd ffffdffffdd ffffd ffffffffd ffffffffd ffffdffffffffd aaaaaaaaaaaa aaaaaaaa aaa</div>
#parent {
width: 300px;
height: 300px;
background-color: #aaa;
margin: auto auto;
overflow: hidden;
}
#child {
width: 200px;
height: 800px;
background-color: #999;
margin: auto auto;
text-align: justify;
position: relative;
top: 0;
-webkit-transition: all 0.5s ease-out;
}
$('#parent').bind('mousewheel', function (e) {
if (!(e.originalEvent.wheelDelta == 120)) {
var top = parseInt($("#child").css("top"));
$("#child").css("top", (top - 100) + "px");
top = parseInt($("#child").css("top"));
if (top <= -500) {
setTimeout(function () {
$("#child").css("top", "-500px");
}, 100);
}
} else {
var top = parseInt($("#child").css("top"));
$("#child").css("top", (top + 100) + "px");
top = parseInt($("#child").css("top"));
if (top >= 0) {
setTimeout(function () {
$("#child").css("top", "0");
}, 100);
}
}
});
THE DEMO
Karl Swedberg has made a jQuery plugin called Smooth Scroll, which sounds like it may be just what you're after.
I had a very simmilar problem. I wanted to change the scroll function of a normal page. I want every scroll to be exactly of an specific height so that the page stops in very specific positions only.
I realized it in the following way:
1. Used Plugins
Download and include the following 2 jQuery plugins and jQuery itself:
2. Mousewheel event
The easiest way is to use the mousewheel plug in that way:
$('body').mousewheel(function(event, delta) { /* code here */ });
The variable delta is then negative or positive, depending if the wheel was scrolled up or down. If you return false I think (!) it disables the normal scroll.
3. Scroll method
In order to scroll the page I used scrollTo, but any other plugin (like Smooth Scroll suggested in the other answer) should also do it.
4. Complete code
Place this in the head part of you HTML-File:
<script type="text/javascript" src="jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="jquery.scrollTo-1.4.2-min.js"></script>
<script type="text/javascript" src="jquery.mousewheel.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('body').mousewheel(function(event, delta) {
# This if might not be very elegant!
if (delta < 0) {
$('body').scrollTo('+=100', 1500 );
}
else
$('body').scrollTo('-=100', 1500 );
return false;
});
});
</script>
5. Demo
I created a demo here: http://pastehtml.com/view/ba0ziusqk.html