Skip to content

Commit bf3b07c

Browse files
committed
Documentation and demo updates.
1 parent f0de324 commit bf3b07c

File tree

5 files changed

+35
-15
lines changed

5 files changed

+35
-15
lines changed

.jscsrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"esnext": true,
32
"requireCurlyBraces": [
43
"if",
54
"else",

demo/bbq.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ <h2>Smooth Scroll jQuery Plugin with Back Button Support</h2>
4545
</div>
4646
<script src="../lib/jquery/jquery.js"></script>
4747
<script src="../src/jquery.smooth-scroll.js"></script>
48+
<script>
49+
// Need this for "newer" versions of jQuery, which have removed $.browser
50+
$.browser = $.browser || {msie: false};
51+
</script>
4852
<script src="../lib/jquery.ba-bbq.js"></script>
4953
<script>
5054
$(document)

demo/hashchange.html

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
text-align: center;
1616
}
1717
</style>
18+
<!-- ↓ Script down below ↓ -->
1819
</head>
1920
<body>
2021
<h2>Smooth Scroll jQuery Plugin with Back Button Support</h2>
@@ -46,24 +47,31 @@ <h2>Smooth Scroll jQuery Plugin with Back Button Support</h2>
4647
<script src="../lib/jquery/jquery.js"></script>
4748
<script src="../src/jquery.smooth-scroll.js"></script>
4849
<script>
50+
// Bind the hashchange event listener
51+
$(window).bind('hashchange', function(event) {
52+
$.smoothScroll({
53+
// Replace '#/' with '#' to go to the correct target
54+
scrollTarget: location.hash.replace(/^\#\/?/, '#')
55+
});
56+
});
57+
4958
$('a[href*="#"]')
5059
.bind('click', function(event) {
51-
var hash = this.hash.replace(/[#<>]/g, '')
60+
// Remove '#' from the hash.
61+
var hash = this.hash.replace(/^#/, '')
5262

53-
if (this.pathname === location.pathname) {
63+
if (this.pathname === location.pathname && hash) {
5464
event.preventDefault();
55-
}
5665

57-
if (hash) {
66+
// Change '#' (removed above) to '#/' so it doesn't jump without the smooth scrolling
5867
location.hash = '#/' + hash;
5968
}
60-
})
61-
62-
$(window).bind('hashchange', function(event) {
63-
$.smoothScroll({
64-
scrollTarget: '#' + location.hash.replace(/^\#\/?/, '')
65-
});
6669
});
70+
71+
// Trigger hashchange event on page load if there is a hash in the URL.
72+
if (location.hash) {
73+
$(window).trigger('hashchange');
74+
}
6775
</script>
6876

6977
</body>

readme.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ You can try a bare-bones demo at [kswedberg.github.io/jquery-smooth-scroll/demo/
3030
* Exclude links if they are within a containing element: `$('#container a').smoothScroll({excludeWithin: ['.container2']});`
3131
* Exclude links if they match certain conditions: `$('a').smoothScroll({exclude: ['.rough','#chunky']});`
3232
* Adjust where the scrolling stops: `$('.backtotop').smoothScroll({offset: -100});`
33-
* Add a callback function that is triggered before the scroll starts: `$('a').smoothScroll({beforeScroll: function() { alert('ready to go!'); }});
33+
* Add a callback function that is triggered before the scroll starts: `$('a').smoothScroll({beforeScroll: function() { alert('ready to go!'); }});`
3434
* Add a callback function that is triggered after the scroll is complete: `$('a').smoothScroll({afterScroll: function() { alert('we made it!'); }});`
35-
* Add back button support by including a history management plugin such as [Ben Alman's BBQ](http://benalman.com/code/projects/jquery-bbq/docs/files/jquery-ba-bbq-js.html). See [demo/bbq.html](demo/bbq.html) for an example of how to implement this.
36-
35+
* Add back button support by using a [`hashchange` event listener](https://developer.mozilla.org/en-US/docs/Web/API/HashChangeEvent). You can also include a history management plugin such as [Ben Alman's BBQ](http://benalman.com/code/projects/jquery-bbq/docs/files/jquery-ba-bbq-js.html) for ancient browser support (IE &lt; 8), but you'll need jQuery 1.8 or earlier. See [demo/hashchange.html](demo/hashchange.html) or [demo/bbq.html](demo/bbq.html) for an example of how to implement.
3736

3837
#### Options
3938

@@ -49,7 +48,7 @@ The following options, shown with their default values, are available for both `
4948
// only use if you want to override default behavior
5049
scrollTarget: null,
5150

52-
// string to use as selector for event delegation (Requires jQuery >=1.4.2)
51+
// string to use as selector for event delegation
5352
delegateSelector: null,
5453

5554
// fn(opts) function to be called before scrolling occurs.
@@ -59,6 +58,9 @@ The following options, shown with their default values, are available for both `
5958
// fn(opts) function to be called after scrolling occurs.
6059
// `this` is the triggering element
6160
afterScroll: function() {},
61+
62+
// easing name. jQuery comes with "swing" and "linear." For others, you'll need an easing plugin
63+
// from jQuery UI or elsewhere
6264
easing: 'swing',
6365

6466
// speed can be a number or 'auto'

src/jquery.smooth-scroll.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@
2727
// fn(opts) function to be called after scrolling occurs.
2828
// `this` is the triggering element
2929
afterScroll: function() {},
30+
31+
// easing name. jQuery comes with "swing" and "linear." For others, you'll need an easing plugin
32+
// from jQuery UI or elsewhere
3033
easing: 'swing',
34+
35+
// speed can be a number or 'auto'
36+
// if 'auto', the speed will be calculated based on the formula:
37+
// (current scroll position - target scroll position) / autoCoeffic
3138
speed: 400,
3239

3340
// coefficient for "auto" speed

0 commit comments

Comments
 (0)