- Smooth Page Scrolling Js Bootstrap
- Smooth Page Scrolling Js Word
- Smooth Page Scrolling Js Css
- Javascript Smooth Scroll
Yet another jQuery based smooth scroll script which smoothly scrolls the web page to a specific point/ID/anchor/DIV within the document. Also has the ability to automatically update the url hash when the smooth scroll is completely finished. How to use it: 1. Create anchor links pointing to the elements with unique IDs. Smooth page scrolling in JavaScript. Ask Question Asked 8 years, 3 months ago. Active 6 years, 11 months ago. Viewed 21k times 6. 6 $ begingroup $ I wrote a. 31 December 2019 Scroll and Shader Effects in Three.js and React. Discover how to use react-three-fiber and combine some common techniques to create a Three.js powered layout with shader effects. Using jQuery To Create Smooth Page Scrolling. In this post I will walk through the tutorial showing you how to create a smooth page scrolling effect allowing you to scroll to the top or bottom of your web page in a smooth action using jQuery.
CSS Properties
align-contentalign-itemsalign-selfallanimationanimation-delayanimation-directionanimation-durationanimation-fill-modeanimation-iteration-countanimation-nameanimation-play-stateanimation-timing-functionbackface-visibilitybackgroundbackground-attachmentbackground-blend-modebackground-clipbackground-colorbackground-imagebackground-originbackground-positionbackground-repeatbackground-sizeborderborder-bottomborder-bottom-colorborder-bottom-left-radiusborder-bottom-right-radiusborder-bottom-styleborder-bottom-widthborder-collapseborder-colorborder-imageborder-image-outsetborder-image-repeatborder-image-sliceborder-image-sourceborder-image-widthborder-leftborder-left-colorborder-left-styleborder-left-widthborder-radiusborder-rightborder-right-colorborder-right-styleborder-right-widthborder-spacingborder-styleborder-topborder-top-colorborder-top-left-radiusborder-top-right-radiusborder-top-styleborder-top-widthborder-widthbottombox-decoration-breakbox-shadowbox-sizingbreak-afterbreak-beforebreak-insidecaption-sidecaret-color@charsetclearclipclip-pathcolorcolumn-countcolumn-fillcolumn-gapcolumn-rulecolumn-rule-colorcolumn-rule-stylecolumn-rule-widthcolumn-spancolumn-widthcolumnscontentcounter-incrementcounter-resetcursordirectiondisplayempty-cellsfilterflexflex-basisflex-directionflex-flowflex-growflex-shrinkflex-wrapfloatfont@font-facefont-familyfont-feature-settingsfont-kerningfont-sizefont-size-adjustfont-stretchfont-stylefont-variantfont-variant-capsfont-weightgridgrid-areagrid-auto-columnsgrid-auto-flowgrid-auto-rowsgrid-columngrid-column-endgrid-column-gapgrid-column-startgrid-gapgrid-rowgrid-row-endgrid-row-gapgrid-row-startgrid-templategrid-template-areasgrid-template-columnsgrid-template-rowshanging-punctuationheighthyphens@importisolationjustify-content@keyframesleftletter-spacingline-heightlist-stylelist-style-imagelist-style-positionlist-style-typemarginmargin-bottommargin-leftmargin-rightmargin-topmax-heightmax-width@mediamin-heightmin-widthmix-blend-modeobject-fitobject-positionopacityorderoutlineoutline-coloroutline-offsetoutline-styleoutline-widthoverflowoverflow-xoverflow-ypaddingpadding-bottompadding-leftpadding-rightpadding-toppage-break-afterpage-break-beforepage-break-insideperspectiveperspective-originpointer-eventspositionquotesresizerightscroll-behaviortab-sizetable-layouttext-aligntext-align-lasttext-decorationtext-decoration-colortext-decoration-linetext-decoration-styletext-indenttext-justifytext-overflowtext-shadowtext-transformtoptransformtransform-origintransform-styletransitiontransition-delaytransition-durationtransition-propertytransition-timing-functionunicode-bidiuser-selectvertical-alignvisibilitywhite-spacewidthword-breakword-spacingword-wrapwriting-modez-indexAbout
Zenscroll is a vanilla JavaScript library that enables animated vertical scrolling to an element or position within your document or within a scrollable element (DIV, etc.). It can also automatically smooth all scrolling within the same page.
Features:
- Smooth animated vertical scrolling.
- Automatic smooth-scrolling on links within the same page, including back and forward navigation (Chrome, Firefox, Opera, Safari Technology Preview).
- Scroll to the top of a specific element.
- Scrolling an element into view, making sure both top & bottom are visible, if possible.
- Scroll to an element and center it on the screen.
- Specify the spacing between the element and the edge of the screen (e.g., for fixed navigation bars and footers).
- Customize the duration of the individual scroll operations.
- Callback when the scrolling is done.
- Can use new browser's built-in smooth-behavior if it's enabled.
- Just 1.4 kilobyte minimized & gzipped.
- No dependencies.
Full support tested and works under:
- Android Browser 2.2+
- Chrome for Android
- Chrome 14+ (probably earlier too)
- Edge
- Firefox 9+
- Internet Explorer 9+
- iOS Safari 4+
- Opera 10.6+ (probably earlier too)
- Safari 4+
- Windows Phone 8.1
- Yandex 14.12
Limited support (programmatic animated scroll in document) tested and works under:
- Firefox 3+
- Internet Explorer 6+
- iOS Safari 3+
Getting Started
Installing Zenscroll
Download Zenscroll and include it into your page. A good place is at the very bottom, just before the closing tag. For example:
Zenscroll will also work if you reference it in the but it is not recommended for performance reasons. Important: You can only call Zenscroll when
document.body
is already available, so don't try to call Zenscroll functions from the .
You can also get Zenscroll via npm:
Enabling native smooth-scrolling in the browser
If you want to leverage the native smooth-scrolling by the browser (currently available in Firefox 36+ and Chrome 49+) then set the scroll-behavior
CSS property to smooth
on the body and on the elements you want to scroll. E.g.,
If this is set, Zenscroll will use built-in smooth-scrolling in capable browsers while still animating the scroll in older browsers. However, note that if you use the native smooth-scrolling then you loose the finer control options that Zenscroll offers: the speed settings of the animation, and the edge offset for links within the page. Only set this CSS property on the body
or on the elements if you don't need this level of control.
Disabling automatic smoothing on local links
If you want to use Zenscroll programmatically but you don't need the automatic smoothing on local links then set window.noZensmooth
to a non-falsy value. In this case the event handler for automatic smoothing is not installed but you can still use everything, like zenscroll.intoView()
, etc.
It's important to set this value before Zenscroll is executed, otherwise the handlers will be installed. So make sure the setting comes before the loading of the script. For example:
(I consider the disabling of the automatic smooth-scrolling a rather rare scenario that's why I install the event handlers by default.)
How to use
1. Automatic smooth-scroll to links within the page
If Zenscroll is included in your page it will automatically animate the vertical scrolling to anchors on the same page. No further setting is required. (Note that it can be disabled, see 1.5.)
Automatic smooth-scrolling works also with content you dynamically load via AJAX, as Zenscroll uses a generic click handler. Since the automatic smooth-scrolling is implemented a progressive enhancement, internal links work in older browsers as well.
1.1. Automatic edge offset adjustment on load
Zenscroll automatically adds the configured edge offset when the page is loaded with a hash suffix. For example, if you navigate to 'http://yoursite.com/#about'
then the scroll position will be not cut sharply at the top edge of the element with id='above'
. Instead, Zenscroll automatically adds the configured edge offset (which is 9 pixels by default, unless you changed it).
No automatic adjustment happens in the following cases:
- If automatic smooth-scroll is disabled via
noZensmooth
or the native smooth-scrolling (see 1.5) - If edge offset was set to zero (e.g.,
zenscroll.setup(null, 0)
).
1.2. Limited support for smooth back & forward navigation
The scroll is also animated when the browser's back and forward buttons or the relevant key combinations are used (or even if the navigation is invoked from JavaScript). Note that although Zenscroll remembers the vertical scroll position, it cannot calculate changes caused by browser window resizing or collapsing/expanding elements, etc.
This functionality requires browser support for history.scrollRestoration
which is available for example in Chrome 46+, Firefox 46+, and Safari Technology Preview.
1.3. Limited support for the hashchange
event and the CSS pseudo :target
The automatic smooth-scroll on local links can also trigger the standard hashchange
event and the CSS pseudo-class :target
but only if you set the edge offset to 0. I had to introduce this limitation because otherwise the scrolling isn't smooth.
So if you need hashchange
or :target
then make sure you execute zenscroll.setup(null, 0)
.
1.4. No support for automatic scroll to elements inside scrollable elements
Zenscroll wants to be a lightweight solution for animated vertical scrolling, that works in most browsers. The automatic link-scrolling is even more focused on anchors that are directly inside the page body. Unfortunately it won't scroll accurately to elements that are inside other scrollable elements (e.g., overflow: auto; max-height: 100px
). Don't expect Zenscroll to re-implement the full functionality of the browser, with the recursive logic of traversing the nested scrollable elements.
So, how to deal with a situation like this? Try one of the following methods:
- Exclude the problematic links from the automatic smooth-scrolling (see 1.6.).
- Or implement the special logic programatically (see 7. Scroll inside a scrollable DIV).
- Or don't use Zenscroll at all. Instead, rely on the browser's built-in scrolling, and enable the native smooth-scrolling (via
body { scroll-behavior: smooth }
) which works in new browsers.
1.5. Disable the automatic smooth-scrolling
Smooth Page Scrolling Js Bootstrap
The automatic smooth-scrolling is completely disabled in the following cases:
- If you set
window.noZensmooth
to a non-falsy value (see above). - In new browsers if the
scroll-behavior
CSS property is set tosmooth
on thebody
(see above). In this case Zenscroll will only enable automatic smooth-scrolling in browsers which don't support this feature yet (e.g., Internet Explorer).
1.6. Exclude a link from the automatic smooth-scrolling
If you want only some of the links to be excluded from the automatic smoothing then do one of the following:
- Add the class
noZensmooth
to the anchor element, for example - Alternatively, start with the path of the page. E.g., instead of writing
use
or
.
2. Scroll to the top of an element
3. Scroll to a specific vertical position
4. Scroll an element into view
5. Scrolls the element to the center of the screen
Note that a zero value for offset is ignored. You can work around this by using zenscroll.toY()
.
6. Set the duration of the scroll
7. Scroll inside a scrollable DIV
If you want to leverage the native smooth-scrolling by the browser (currently available in Firefox 36+ and Chrome 49+) then set the scroll-behavior
CSS property to smooth
on the body and on the elements you want to scroll. E.g.,
If this is set, Zenscroll will use built-in smooth-scrolling in capable browsers while still animating the scroll in older browsers. However, note that if you use the native smooth-scrolling then you loose the finer control options that Zenscroll offers: the speed settings of the animation, and the edge offset for links within the page. Only set this CSS property on the body
or on the elements if you don't need this level of control.
Disabling automatic smoothing on local links
If you want to use Zenscroll programmatically but you don't need the automatic smoothing on local links then set window.noZensmooth
to a non-falsy value. In this case the event handler for automatic smoothing is not installed but you can still use everything, like zenscroll.intoView()
, etc.
It's important to set this value before Zenscroll is executed, otherwise the handlers will be installed. So make sure the setting comes before the loading of the script. For example:
(I consider the disabling of the automatic smooth-scrolling a rather rare scenario that's why I install the event handlers by default.)
How to use
1. Automatic smooth-scroll to links within the page
If Zenscroll is included in your page it will automatically animate the vertical scrolling to anchors on the same page. No further setting is required. (Note that it can be disabled, see 1.5.)
Automatic smooth-scrolling works also with content you dynamically load via AJAX, as Zenscroll uses a generic click handler. Since the automatic smooth-scrolling is implemented a progressive enhancement, internal links work in older browsers as well.
1.1. Automatic edge offset adjustment on load
Zenscroll automatically adds the configured edge offset when the page is loaded with a hash suffix. For example, if you navigate to 'http://yoursite.com/#about'
then the scroll position will be not cut sharply at the top edge of the element with id='above'
. Instead, Zenscroll automatically adds the configured edge offset (which is 9 pixels by default, unless you changed it).
No automatic adjustment happens in the following cases:
- If automatic smooth-scroll is disabled via
noZensmooth
or the native smooth-scrolling (see 1.5) - If edge offset was set to zero (e.g.,
zenscroll.setup(null, 0)
).
1.2. Limited support for smooth back & forward navigation
The scroll is also animated when the browser's back and forward buttons or the relevant key combinations are used (or even if the navigation is invoked from JavaScript). Note that although Zenscroll remembers the vertical scroll position, it cannot calculate changes caused by browser window resizing or collapsing/expanding elements, etc.
This functionality requires browser support for history.scrollRestoration
which is available for example in Chrome 46+, Firefox 46+, and Safari Technology Preview.
1.3. Limited support for the hashchange
event and the CSS pseudo :target
The automatic smooth-scroll on local links can also trigger the standard hashchange
event and the CSS pseudo-class :target
but only if you set the edge offset to 0. I had to introduce this limitation because otherwise the scrolling isn't smooth.
So if you need hashchange
or :target
then make sure you execute zenscroll.setup(null, 0)
.
1.4. No support for automatic scroll to elements inside scrollable elements
Zenscroll wants to be a lightweight solution for animated vertical scrolling, that works in most browsers. The automatic link-scrolling is even more focused on anchors that are directly inside the page body. Unfortunately it won't scroll accurately to elements that are inside other scrollable elements (e.g., overflow: auto; max-height: 100px
). Don't expect Zenscroll to re-implement the full functionality of the browser, with the recursive logic of traversing the nested scrollable elements.
So, how to deal with a situation like this? Try one of the following methods:
- Exclude the problematic links from the automatic smooth-scrolling (see 1.6.).
- Or implement the special logic programatically (see 7. Scroll inside a scrollable DIV).
- Or don't use Zenscroll at all. Instead, rely on the browser's built-in scrolling, and enable the native smooth-scrolling (via
body { scroll-behavior: smooth }
) which works in new browsers.
1.5. Disable the automatic smooth-scrolling
Smooth Page Scrolling Js Bootstrap
The automatic smooth-scrolling is completely disabled in the following cases:
- If you set
window.noZensmooth
to a non-falsy value (see above). - In new browsers if the
scroll-behavior
CSS property is set tosmooth
on thebody
(see above). In this case Zenscroll will only enable automatic smooth-scrolling in browsers which don't support this feature yet (e.g., Internet Explorer).
1.6. Exclude a link from the automatic smooth-scrolling
If you want only some of the links to be excluded from the automatic smoothing then do one of the following:
- Add the class
noZensmooth
to the anchor element, for example - Alternatively, start with the path of the page. E.g., instead of writing
use
or
.
2. Scroll to the top of an element
3. Scroll to a specific vertical position
4. Scroll an element into view
5. Scrolls the element to the center of the screen
Note that a zero value for offset is ignored. You can work around this by using zenscroll.toY()
.
6. Set the duration of the scroll
7. Scroll inside a scrollable DIV
Anything you can do within the document you can also do inside a scrollable DIV or other element. You just need to instantiate a new scroller for that element. I will also fall back by default to the native browser smooth-scrolling if available (which can be overridden via setup()
).
Important: the container DIV must have its position
CSS property set to relative
, absolute
or fixed
. If you want to keep it in the normal document flow, just assign position: relative
to it via a class or a style
attribute, like in the example below:
Obviously you can use all other scroll functions and parameters with the scrollable container. Two more examples:
8. Execute something when the scrolling is done
You can provide a callback function to all four scroll functions, which is executed when the scroll operation is finished. For example, you change some UI elements but first you want to make sure that the relevant elements are visible.
Note that the callback is immediately invoked if the native scroll-smoothing is enabled (see above).
If you look at the source code of the examples under Scroll inside a scrollable DIV they are actually implemented like this:
So first the container (with ITEM 1 to ITEM 7) is scrolled into view if necessary, and then the scrolling inside the container is performed. Try scrolling out the above container and then hit one of the ‘Play' buttons above to see how it works.
This works with all four scrolling functions. The onDone
parameter is always the last parameter:
to(element, duration, onDone)
toY(y, duration, onDone)
intoView(element, duration, onDone)
center(element, duration, offset, onDone)
9. Change settings
It's easy to change the basic parameters of scrolling:
- You can set the default value for duration. This will be valid for internal scrolls and all your direct scroll calls where you don't specify a duration.
- Change the edge offset (the spacing between the element and the screen edge). If you have a fixed navigation bar or footer bar then set the offset to their height.
You can change custom scrollers similarly:
If you don't want to change a value just omit the parameter or pass null
. For example, the line below sets the default duration, while leaving other settings unchanged:
Sets the the spacing between the edge of the screen (or a DIV) and the target element you are scrolling to, while leaving the default duration unchanged:
The function always returns the current values in an object, so even if no parameters are passed you can obtain the current settings:
10. Additional functions
Smooth Page Scrolling Js Word
To check whether a scroll is being performed right now:
To stop the current scrolling:
To get the current vertical scrolling position:
To get the top position of an element within the body or a scroller:
Impotant: the returned value is without edge offset, and without recurcursively calculating nested scrollable containers.
License
Public Domain. You can do with it whatever you want and I am not responsible for anything.
Smooth Page Scrolling Js Css
About Me
My name is Gabor Lenard. You can find me on Twitter as @zengabor. I am a product designer & maker, currently building zenvite.com. It never ceases to amaze me how people share their knowledge and what they make. Their work empowers us all. This here, however humble, is my way of contributing to this community & the wonderful continuum we call the world wide web.
Javascript Smooth Scroll
Other projects by me:
- Zenfonts, a tiny JavaScript helper for @font-face web font loading.
- Zenvite.com: Create invitation pages & get everybody on the same page.