Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add animationTarget option to allow to set animation element explicitly. #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/jquery.plusanchor.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 38 additions & 34 deletions js/jquery.plusanchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,11 @@
var $this = $(this); // Anchor el
var href = $this.attr('href');
var $name = $('a[name="' + href.substring(1) + '"]');

if ( $(href).length ){

// onSlide callback
if ( base.options.onSlide && typeof( base.options.onSlide ) === 'function' ) {
base.options.onSlide( base );
};

// End onSlide callback
base.$el.animate({
scrollTop: $(href).offset().top + base.options.offsetTop
}, base.options.speed, base.options.easing);

} else if ( $name.length ){

// onSlide callback
if ( base.options.onSlide && typeof( base.options.onSlide ) === 'function' ) {
base.options.onSlide( base );
};

// End onSlide callback
base.$el.animate({
scrollTop: $name.offset().top + base.options.offsetTop
}, base.options.speed, base.options.easing);


if ( $(href).length ) {
base.animateScrolling($(href));
} else if ( $name.length ) {
base.animateScrolling($name);
}
}
}
Expand All @@ -50,16 +30,21 @@
var base = this;
var htmlEl = document.getElementsByTagName('html');
var bodyEl = document.body;
// If the user selects body, make sure to use 'html, body' for
// cross browser compatibility reasons
base.el = el;
base.$el = (el === htmlEl || el === bodyEl) ? $('html, body') : $(el);
base.$el.data('plusAnchor', base); // Add a reverse reference to the DOM object

function _constructor() {
// If the user selects body, make sure to use 'html, body' for
// cross browser compatibility reasons
base.el = el;
base.$el = (el === htmlEl || el === bodyEl) ? $('html, body') : $(el);
base.$animationTarget = options.animationTarget ? $(options.animationTarget) : base.$el;
base.$el.data('plusAnchor', base); // Add a reverse reference to the DOM object
base.animateScrolling = animateScrolling;

var anchorSelector = 'a[href^="#"]';

// Check for plusanchor disable
if (options === false) {
base.$el.find('a[href^="#"]').off('click.plusanchor');
base.$el.find(anchorSelector).off('click.plusanchor');
return;
};

Expand All @@ -73,11 +58,29 @@
// End onInit callback

if (!base.options.performance) {
base.$el.on('click', 'a[href^="#"]', scrollToEvent(base));
base.$el.on('click', anchorSelector, scrollToEvent(base));
} else {
base.$el.find('a[href^="#"]').on('click.plusanchor', scrollToEvent(base));
base.$el.find(anchorSelector).on('click.plusanchor', scrollToEvent(base));
}
}; // _constructor

function animateScrolling($scrollTarget) {
// onSlide callback
if (base.options.onSlide && typeof (base.options.onSlide) === 'function') {
base.options.onSlide(base);
};

var scrollTop = ($scrollTarget ? $scrollTarget.offset().top : 0) +
base.options.offsetTop;

// End onSlide callback
base.$animationTarget.animate({
scrollTop: scrollTop
},
base.options.speed,
base.options.easing
);
}

// Run initializer
_constructor();
Expand All @@ -89,7 +92,8 @@
speed: 1000, // Int: The speed, in miliseconds, it takes to complete a slide
onInit: null, // Function: Callback function on plugin initialize
onSlide: null, // Function: Callback function that runs just before the page starts animating
performance: false // Boolean: Toggles between click and delegate events.
performance: false, // Boolean: Toggles between click and delegate events.
animationTarget: null //String|DOM Element: Selector or DOM element to apply animation to. Keep unspecified, if the target element and animation element are the same.
};

$.fn.plusAnchor = function(options){
Expand Down