Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enforce a "smooth scrolling" rule for mousewheel, jQuery?

Tags:

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, and/or grabbing the scrollbar. What I want would be to overcome the particular default method of page scrolling.
As it currently works the page jumps right to x# of pixels down per 'notch' that is progressed on the scrollwheel. Or goes directly to where you drag the scroll bar. What i'm looking for is a simple extension to jquery that can apply certain scrolling rules.The principals are simple. Imposed acceleration, this would prevent the page from moving too fast without first accelerating. settable as a rate of in px/sec- with the option to apply easing functions...There is a maximum px/sec that the page can slide/move/drag. and thirdly is a deceleration rule, applied as the page approaches it's destination position (in %, px?). This may have to be calculated in one of many ways, and may be trickier. ie when scrolling the last 25 pixels to the destination position, the deceleration applys. There's more... The main concern I'd want to prepare for would be ensuring that small page scrolls we're supported fully and not glitchy.
What sort of jQuery approaches could be used to control the document in this way? _kyle

Update::: Thanks For following this Q, if you are. Great News. Found a great plugin that hopefully can be handled to be supporting the desired effects, yo! I've implemented a whole page container and used this nifty jScrollPane script to commandeer if you will the scrolling of the page http://jscrollpane.kelvinluck.com/fullpage_scroll.html

There is already a big difference in the performance of the page. Each scroll notch from the wheel is a third to a half of the browsers native scroll notch, so it moves slower, which is fine, surly that's adjustable.
We still have the stall-stall-stall method of page movement, though.

I myself write javascript, but more it's like I re-write it. What I think needs to be done is a "queue" built of pixels to be scrolled along a page, with the total time compounded: and then an animation footprint defined and executed as three phases, an in easing acceleration, maxscrollspeed phase, and decel phase.

Could anybody offer any suggestions as to how we

  1. Detatch the mousescrollwheel from its native function of scrolling the page.

  2. listen for notches of the mousescroll; and in the event of a notch: initialize the corefunction to start the movement of the page, but also listen for and add additional notch clicks to "queue" that is re-calulated and applied to the scroll of the window replacing the previous totaldistancetoscroll, before calculating the next totaldistancetoscroll, which provides a way to anticipate the destination and decelerate. The corefunctions to start movement wouldn't want to be restarted, cause multiple notch clicks will probably happen while accelerating, but just a recalculated when and where to decelerate should be applied.

Sorry again for not posting any actual code yet, not exactly sure where to start and was hoping someone might know if mwintent will work for this and if so: might also have some ideas how to apply a deceleration to the scroll, which seems to be the only two difference between the desired effect and the state of the current plug-ins that are similar.

like image 758
bnbgnoise Avatar asked Apr 06 '11 02:04

bnbgnoise


People also ask

How do I make my anchor link scroll smoother?

To achieve a smooth scrolling effect for anchor links, add a scroll modifier—block T178 from the "Other" category to the bottom of your page. Now, when you click an anchor link, the transition will be smoothly animated.

How do I smooth a scroll to an element?

To scroll to an element, just set the y-position to element. offsetTop . The SmoothScroll.

How do I fix smooth scrolling?

Anyway, to disable smooth scrolling in Chrome, follow these steps: Step 1: Type in chrome://flags/#smooth-scrolling in Chrome's address bar and press Enter. Step 2: To turn the feature off, click on the disable link. Then, click on a restart button to complete the process.

Why is scrolling not smooth?

You may have a problem with a system setting or a graphics driver if you experience choppy scrolling on Web pages. The choppy page display could mean that your computer's touch device or mouse is set at too high of a scrolling interval or that the computer's graphics card isn't able to process graphics fast enough.


1 Answers

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.

like image 70
simo Avatar answered Dec 04 '22 19:12

simo