Hello, i would like to have 2 animations working together, the smooth scroll to anchor JS and the custom JS function i coded.
i got a code from this forum for the scroll animation (and edited a bit to suit my case).
jQuery(document).ready(function($) {
var $body = $('body');
var bodyHeight = $body.outerHeight();
var adminbarHeight = $('#wpadminbar').outerHeight();
var navbarFixedTopHeight = $('.hm1.x-bar').outerHeight();
var locHref = location.href;
var locHashIndex = locHref.indexOf('#');
var locHash = locHref.substr(locHashIndex);
var dragging = false;
$body.on('touchmove', function() {
dragging = true;
} );
$body.on('touchstart', function() {
dragging = false;
} );
//
// Calculate the offset height for various elements and remove it from
// the element's top offset so that fixed elements don't cover it up.
//
function animateOffset( element, ms, easing ) {
$('html, body').animate({
scrollTop: $(element).offset().top - adminbarHeight - navbarFixedTopHeight + 1
}, ms, easing);
}
//
// Page load offset (if necessary).
//
$(window).load(function() {
if ( locHashIndex !== -1 && $(locHash).length ) {
animateOffset(locHash, 1, 'linear');
}
});
//
// Scroll trigger.
//
$('a[href*="#"]').unbind('touchend click').on('touchend click', function(e) {
console.log($('.hm1.x-bar-fixed').outerHeight());
href = $(this).attr('href');
notComments = href.indexOf('#comments') === -1;
if ( href !== '#' && notComments ) {
var theId = href.split('#').pop();
var $el = $('#' + theId);
if ( $el.length > 0 ) {
e.preventDefault();
if (dragging) {
return;
}
animateOffset($el, 850, 'easeInOutExpo');
}
}
});
});
and i coded this:
jQuery(document).ready(function($, event){
if ( $(window).width() > 438) {
/* $('header.x-masthead, html').addClass('masthead-bckgr-color'); */
$('.x-main').addClass('effect transformed');
$('.x-main').toggleClass('transformed',false);
$('.behind-header').hide();
$(window).on('click scroll',dynamicForm);
function dynamicForm(e){
switch(e.type){
case 'click':{
if($(e.target).closest('.open-btn').length){
$('.effect').toggleClass('transformed',true);
$('.navbar').css('height','inherit');
$('.behind-header').show();
$('.behind-header').removeClass('remove-transition');
$('.behind-header').addClass('add-transition');
$('.behind-header').css('display','show');
} else if($(e.target).closest('.close-btn').length){
$('.effect').toggleClass('transformed',false);
$('.behind-header').removeClass('add-transition');
$('.behind-header').addClass('remove-transition');
}else{
if(!$(e.target).closest('behind-header')){
$('.effect,').toggleClass('transformed',false);
$('.behind-header').removeClass('add-transition');
$('.behind-header').addClass('remove-transition');
}
}
break;
}
default:{
if(!$(e.target).hasClass('close-btn') || !$(e.target).hasClass('open-btn') ){
$('.effect').toggleClass('transformed',false);
$('.behind-header').removeClass('add-transition');
$('.behind-header').addClass('remove-transition');
}
break;
}
}
}
} else{
$('.effect').removeClass('transformed');
}
});
i would like to have both of them happen simultaneously, i mean, i want that if the user clicks a button with an id “#contact” and the offset != 0, it will first scroll up to the top and then trigger the next function (the one i coded).
if you go to my site i’m almost there, because it does scroll to the top but you have to click again for it to open.
i thought that i could maybe trigger another click once the window has scrolled to the top or something but i don’t know if that would be the best approach to solve the issue.
could you please point me to the right direction? i have experimented a bit with the code and you can head to the site’s header ( standard header) and check what i’ve done so far.