Page scroll to id
Code examples & short tutorials
Using offsets
You can offset the scroll-to position via the offset
option parameter and/or via the data-ps2id-offset
html attribute.
offset
option parameter provides a general offset for all the links handled by the plugin. For example, setting offset value to 50 will stop page scrolling 50 pixels before reaching the target
$("a[rel='m_PageScroll2id']").mPageScroll2id({ offset:50 });
data-ps2id-offset
attribute provides link specific offsets. The data attribute overwrites the offset
option and is useful when you need to set a different offset for a link
<a href="#id" rel="m_PageScroll2id" data-ps2id-offset="100">link</a>
When the link above is clicked, it will stop page scrolling 100 pixels before reaching the target.
The offset value can be a positive or negative number, a string representing an element selector, a js or jquery object, a function or an array of values.
Integer
Offsets scroll-to position by x amount of pixels (positive or negative number). Examples:
$("a[rel='m_PageScroll2id']").mPageScroll2id({ offset:50 });
<a href="#id" rel="m_PageScroll2id" data-ps2id-offset="100">link</a>
$.mPageScroll2id("scrollTo","#id",{ offset:-50 });
String
Offsets scroll-to position by element height/width/position. Examples:
$("a[rel='m_PageScroll2id']").mPageScroll2id({ offset:"#id" });
<a href="#id" rel="m_PageScroll2id" data-ps2id-offset=".class-name">link</a>
js/jquery object
Offsets scroll-to position by element height/width/position. Examples:
$("a[rel='m_PageScroll2id']").mPageScroll2id({ offset:document.getElementById("id") });
$("a[rel='m_PageScroll2id']").mPageScroll2id({ offset:$("#id") });
Function
Offsets scroll-to position via function. Examples:
$("a[rel='m_PageScroll2id']").mPageScroll2id({ offset:function(){ return offsetFn(); } }); function offsetFn(){ var myOffsetY=50, myOffsetX=100; return [myOffsetY,myOffsetX]; }
Array
Offsets vertical (y) and horizontal (x) scroll-to positions separately. Examples:
$("a[rel='m_PageScroll2id']").mPageScroll2id({ offset:{"y":"50","x":"100"} });
<a href="#id" rel="m_PageScroll2id" data-ps2id-offset='{"y":"#id","x":"0"}'>link</a>
$("a[rel='m_PageScroll2id']").mPageScroll2id({ offset:{"y":document.getElementsByTagName("nav") || "ul.nav","x":"0"} });
User defined callbacks
You can trigger your own js function(s) by calling them inside onStart
and onComplete
option parameters. For example:
$("a[rel='m_PageScroll2id']").mPageScroll2id({ onStart:function(){ myCustomFn1(); }, onComplete:function(){ myCustomFn2(); } }); function myCustomFn1(){ console.log(mPS2id.target.attr("id")); } function myCustomFn2(){ console.log(mPS2id.scrollTo.y); }
Returning values
The script returns the following values and objects
mPS2id.trigger
– the event which triggered page scrolling. Returned values: “selector” (click), “scrollTo” (method)mPS2id.clicked
– the element that was clicked as jQuery objectmPS2id.target
– the target element as jQuery objectmPS2id.scrollTo.y
– the vertical scroll-to position (pixels)mPS2id.scrollTo.x
– the horizontal scroll-to position (pixels)
Using Page scroll to id with deep linking plugins
There are many solutions for deep linking via javascript. The archive contains examples of using Page scroll to id with jquery.address and jquery-hashchange.
Normally, you’ll use plugin’s scrollTo method to trigger page scroll in hash-change events. If you need to disable the click events of selectors, set clickEvents
to false
$("a[rel='m_PageScroll2id']").mPageScroll2id({ clickEvents:false }); $(window).hashchange(function(){ var loc=window.location, to=loc.hash.split("/")[1] || "#top"; $.mPageScroll2id("scrollTo",to,{ clicked:$("a[href='"+loc+"'],a[href='"+loc.hash+"']") }); });
Dynamic content
When adding links and/or sections to your page dynamically (e.g. via ajax, js click events etc.) you may need to re-call mPageScroll2id function after your new content is fully loaded: $.mPageScroll2id();
This ensures the script will highlight your newly added links and targets correctly. If you don’t use plugin’s highlight feature, you can skip mPageScroll2id function call.
Links highlighting
The plugin highlights links automatically. When the page is scrolled (by clicking a link, mouse-wheel, touch-swipe etc.), the script checks if a target element is within the viewport and adds a highlight class (default: mPS2id-highlight
) to the corresponding link(s).
You can change the default highlight class to your own, through the highlightClass
option parameter
$("a[rel='m_PageScroll2id']").mPageScroll2id({ highlightClass:"highlighted-menu-item" });
By default, all links handled by the plugin are eligible for highlighting. Normally, you’d want to highlight only certain links such as those in navigation menus and not others like back-to-top. This can be achieved either by styling only the CSS selector you want, for example:
.menu a.mPS2id-highlight{ background: #ff0; }
or by inserting the selectors subset in the highlightSelector
option parameter
$("a[rel='m_PageScroll2id']").mPageScroll2id({ highlightSelector:".menu a" });
The plugin also provides a way of manually setting the highlight positions of each target element. If you need to set a specific area (in pixels) at which the highlight class is added, use the option parameter target_
followed by the number of your target within your document
$("a[rel='m_PageScroll2id']").mPageScroll2id({ target_1:{ from:0, to:-799 }, target_2:{ from:-800, to:-1599 }, target_3:{ from:-1600, to:-2399 } });
The target_
option gets the values { from, to, fromX, toX }
When multiple targets are within the viewport, more than one links will be highlighted simultaneously. If you don’t want this behavior, set forceSingleHighlight
option parameter to true
$("a[rel='m_PageScroll2id']").mPageScroll2id({ forceSingleHighlight:true });
Alternatively, you can target the first and last highlighted elements by using the classes .mPS2id-highlight-first
and .mPS2id-highlight-last
.
Plugin home Project on GitHub
Hello –
Still loving this plugin. One quick question – is there a way to exclude a specific anchor link? I read in another thread to add an exclusion (see below) but the code below didn’t do the trick.
a[href$=’#sampleexercises’]
The anchor link is accessed from a different page and creates a choppy experience where the user is taken to the anchor link, then to the top, then has a smooth scroll down. Would ideally just get rid of the smooth scroll for this one particular link.
Thanks!
Hello,
Are you using the WordPress plugin? If yes, you probably need to disable the “Enable different pages scrolling on all links” option in plugin settings. Let me know.
Hi Manos
I have used your script successfully on a few occasions thank you!
I think I may have asked this question before – apologies if I have.
Can I set the plugin to smooth scroll automatically to an id (with offset) a few seconds after a page has loaded (without the user having to click on a link)?
Thanks again
Hi again – I have just found your earlier reply from 2020 – so no need to reply again – unless something has changed in your latest version.
Thanks again 🙂
hello,
is it possible to use this plugin to scroll inside a container?
let me explain, my scrollable div(content-ajax) is inside a div (wrapper), therefore the scroll to id function doesn’t work since it’s the body element that gets animated by the script…
#wrapper { width: 100%; height: 100vh; overflow: scroll; } #content-ajax {}
Is there a workaround?
thanks
Unfortunately, no. The plugin works strictly on the root element (html/body).
Hi, can anyone please help me fixing the issue. I have a website https://maithrinet.healthyhomefood.com/ with the 5 menus (Home, About, Solutions, Services, Contact Us) on the header. On clicking Solutions, Services and contact Us links, the page scrolls to the particular sections on the Home page and when the page scrolls the header gets stick on the top. The problem is on menu click the complete section is not visible properly as the section hides behind the sticky header partially. How can i make the page scroll to the exact location and not hide any content behind the sticky header. I have used page scroll to id and Sticky Header Effects for Elementor plugins to achive the effects.
Hello Shwetha,
I just checked your site. You need to utilize the offset option in plugin settings to offset the scrolling according to your sticky header. The best way to do this is to use your sticky header element selector.
Just go to “Page scroll to id” settings and set the “Offset” option value to:
.she-header-yes
Click save changes and test your page links 😉
Let me know if it works.
Hey Manos,
Thanks a lot for fixing the issue.
if am in about page here i clicked button its going to home page #section, For this #section need to add class using my custom script or ?
Please post your page URL so I can check it.
Hi I thing im stuck a little
Without this class
.navigation-top .current-menu-item > a, .navigation-top .current_page_item > a .mPS2id-highlight {color: #222;
}
All my menu items behave like clicked links and have a color #767676;
When i add
.navigation-top .current-menu-item > a, .navigation-top .current_page_item > a .mPS2id-highlight {color: #222;
}
they have #222; color no matter what.
and my end gol is if the menu is untouch the colour shuld be #222; on mouse click and realase it should be #222; but on hover mouse i would like to hane #767676;.
I do something wrong clearly.
oh
I have no idea how to force slow scroll after clicking home menu buton.
no matter I use custom link or default menu option which is site, back to top on home is instant not floating smooth.
Have a great day. Thank you in advance.
Sorry for my crappy english
Hi,
You need to exclude the
:hover
and optionally the.mPS2id-highlight
selectors in your CSS. There’s also an error in your second selector as there seems to be a space betweena
and.mPS2id-highlight
. So, change your CSS to:.navigation-top .current-menu-item > a:not(:hover), .navigation-top .current_page_item > a:not(:hover){ color: #222; }
If you also want to have the
#767676
color on the highlighted links while scrolling the page, change the CSS to:.navigation-top .current-menu-item > a:not(:hover):not(.mPS2id-highlight), .navigation-top .current_page_item > a:not(:hover):not(.mPS2id-highlight){ color: #222; }
For the “Home” link, make it a custom link and set its URL to:
https://sopot-homeyapartment.pl/#top
Let me know if everything it works 🙂
Thank you so much for the outstanding information. It means a lot for me 🙂
Hello, the following error shows up in the admin area
Array to string conversion –> wp-includes/pluggable.php:2279 –> Plugin: page-scroll-to-id
Is there a workaround?
have a good day
Hello,
This is a known issue which will be fixed in the next plugin version (1.7.8). If you don’t want to wait, you can download the development version from the link below and use it replacing the current plugin version you have:
https://downloads.wordpress.org/plugin/page-scroll-to-id.zip
You can keep using the development version and update to version 1.7.8 when it becomes available, as you normally do.
Let me know if you need more help.