Skip to content

Commit cf8cfa4

Browse files
author
Kevin Olson
committed
add offset scrolling ability
1 parent ac6ef74 commit cf8cfa4

File tree

5 files changed

+54
-140
lines changed

5 files changed

+54
-140
lines changed

plugins/system/autoscrollspy/assets/autoscrollspy.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,31 @@ function attemptForceSticky(){
150150
parent.style.top = '0';
151151
parent.style.zIndex = '1000';
152152

153-
}
153+
}
154+
155+
156+
//handle offset scrollOffsetTop attribute given from params
157+
//whenever a local link is clicked, we need to scroll to the right position
158+
//joomla provides scrollOffsetTop as const so we can use that
159+
//add event listener to all .autoss-local-link (they are a elems)
160+
161+
if(scrollOffsetTop != 0){
162+
const localLinks = document.querySelectorAll('.autoss-local-link');
163+
for (var i = 0; i < localLinks.length; i++) {
164+
localLinks[i].addEventListener('click', function(e) {
165+
e.preventDefault();
166+
console.log('clicked' + this.getAttribute('href'));
167+
var href = this.getAttribute('href');
168+
var id = href.replace('#', '');
169+
var el = document.getElementById(id);
170+
var pos = el.offsetTop - scrollOffsetTop;
171+
window.scrollTo({
172+
top: pos,
173+
behavior: 'smooth'
174+
});
175+
});
176+
}
177+
}
178+
179+
180+

plugins/system/autoscrollspy/autoscrollspy.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class PlgSystemAutoScrollSpy extends CMSPlugin
2222
public function onContentBeforeDisplay($context, &$article, &$params, $limitstart){
2323

2424

25-
//get params for plugin from plugin settings without new registery
25+
//setup
2626
$plugin = PluginHelper::getPlugin('system', 'autoscrollspy');
2727
$this->params = new Registry($plugin->params);
2828

@@ -43,16 +43,20 @@ public function onContentBeforeDisplay($context, &$article, &$params, $limitstar
4343
$wam->registerAndUseScript('plg_system_autoscrollspy', $plugin_path.'/assets/autoscrollspy.js', [], ['defer' => 'true']);
4444
}
4545

46-
47-
46+
/* floatpanel props*/
4847
$floatpanel_position = $this->params->get('floatpanel_position', 'left');
4948
$floatpanel_width = $this->params->get('floatpanel_width', '200px');
5049
$floatpanel_offset_top = $this->params->get('floatpanel_offset_top', '250px');
5150
$floatpanel_paneltitle = $this->params->get('floatpanel_paneltitle', '');
5251
$floatpanel_autocollapse_width = $this->params->get('floatpanel_autocollapse_width', '768');
5352
$floatpanel_collapse_toggler_type = $this->params->get('floatpanel_collapse_toggler_type', 'fa-button');
5453

55-
54+
// handle offset of scroll to heading (pass to js)
55+
$scroll_offset_top = $this->params->get('scroll_offset_top', '0');
56+
$wam->addInlineScript('
57+
const scrollOffsetTop = '.$scroll_offset_top.';
58+
', ['type' => 'text/javascript']);
59+
5660

5761
$list_elem = 'ul';
5862
$li = 'li';
@@ -66,8 +70,8 @@ public function onContentBeforeDisplay($context, &$article, &$params, $limitstar
6670
$list_elem = 'nav';
6771
$li = 'span';
6872
$list_elem_class = 'nav nav-pills flex-column';
69-
$a_class = 'nav-link';
70-
$a2_class = 'nav-link ms-2';
73+
$a_class = 'nav-link autoss-local-link';
74+
$a2_class = 'nav-link autoss-local-link ms-2';
7175
}
7276

7377
//make sure we're on an article page
@@ -88,10 +92,7 @@ public function onContentBeforeDisplay($context, &$article, &$params, $limitstar
8892

8993
//get contents of page
9094

91-
$article_text = $article->text;
92-
93-
94-
95+
$article_text = $article->text;
9596
$count = 0;
9697

9798
if($top_is_title == 1){

plugins/system/autoscrollspy/autoscrollspy.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@
4646
<option value="1">JYES</option>
4747
<option value="0">JNO</option>
4848
</field>
49+
50+
<field
51+
name="scroll_offset_top"
52+
type="number"
53+
label="PLG_SYSTEM_AUTO_SCROLLSPY_SCROLL_OFFSET_TOP"
54+
description="PLG_SYSTEM_AUTO_SCROLLSPY_SCROLL_OFFSET_TOP_DESC"
55+
default="0"
56+
required="true"
57+
/>
58+
59+
4960
<field
5061
name="level1selector"
5162
type="list"

plugins/system/autoscrollspy/language/en-GB/plg_system_autoscrollspy.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@ PLG_SYSTEM_AUTOSCROLLSPY_FLOATPANEL_COLLAPSETOGGLETEXT = "Article Contents"
3737
PLG_SYSTEM_AUTO_SCROLLSPY_FIELDSTICKY_NOTE_CONFIG="Sticky Configuration Note"
3838
PLG_SYSTEM_AUTO_SCROLLSPY_FIELDSTICKY_NOTE_CONFIG_DESC="This <strong>attempts</strong> to make the module sticky, so it sticks to the top of the page as user scrolls. This plugin has no idea what template you're using or how it works. So to achieve this, it sets a parent container of the module to position:sticky. The level selection number below is the number of levels between the scroll spy feature and the module container. Usually it is 1, 2, or 3. If none of these work, you may need to change template settings or contract help."
3939
PLG_SYSTEM_AUTO_SCROLLSPY_STICKY_CONTAINER_PARENT_LEVEL="Sticky Container Parent Level"
40-
PLG_SYSTEM_AUTO_SCROLLSPY_STICKY_CONTAINER_PARENT_LEVEL_DESC="The number of levels between the scroll spy feature and the module container. Usually it is 1, 2, or 3. Change value, save plugin, refresh page on front end, and try again until it hopefully works."
40+
PLG_SYSTEM_AUTO_SCROLLSPY_STICKY_CONTAINER_PARENT_LEVEL_DESC="The number of levels between the scroll spy feature and the module container. Usually it is 1, 2, or 3. Change value, save plugin, refresh page on front end, and try again until it hopefully works."
41+
42+
PLG_SYSTEM_AUTO_SCROLLSPY_SCROLL_OFFSET_TOP="Scroll Offset Top (px)"
43+
PLG_SYSTEM_AUTO_SCROLLSPY_SCROLL_OFFSET_TOP_DESC="The offset from the top of the page to scroll down to when clicking a link. Default is 0. Increase this if you have a sticky header or something else that covers the top of the page."

plugins/system/autoscrollspy/media/autoscrollspy.js

Lines changed: 0 additions & 128 deletions
This file was deleted.

0 commit comments

Comments
 (0)