Tagged: sticky header
-
AuthorPosts
-
March 30, 2022 at 4:12 pm #1346608
Dear Enfold Support
On my homepage (see link in Private Content) I have an internal link called “Webcams” to a destination anchor on another page. The destination anchor (an id attribute) is part of an h2 title tag (<h2 id=”webcams”>Webcams</h2>). If I follow that link, the corresponsive page opens at the position of the destination anchor (h2 title) but it is covered by the sticky header on bigger screens with desktop browsers like Firefox or Chrome. I expect, that the title should be below the sticky header. I made a short screen video (see link in Private Content) that shows what I mean. It isn’t a problem on small screens because the sticky header is turned off.
That issue also seems to be quite similar to another still open topic I’ve started because somehow accordion toggles are also destination anchors:
https://kriesi.at/support/topic/toggles-of-accordions-are-covered-by-the-sticky-headerThere, Enfold’s support member Ismael has provided the following code for the child theme’s functions.php to control the scroll offset. I played a little bit with the offset value from – to + but unfortunately that code doesn’t work reliably so far in both cases.
function avf_header_setting_filter_mod($header) { if(!wp_is_mobile()) { $header['header_scroll_offset'] = $header['header_scroll_offset'] + 50; } return $header; } add_filter('avf_header_setting_filter', 'avf_header_setting_filter_mod', 9999, 1);
That code is still active on my child theme’s functions.php with the offset value + 50. I’ve already mentioned in the other topic that Ismael’s PHP function might not be loaded or initialized properly, because it rarely works. In this case for example, if I reload the page with Ctrl + F5 in Firefox, the h2 title is below the sticky header according to the offset value. Strangely enough this trick rarely works in Chrome.
Anyway, Enfold’s sticky header is driving me really frustrated. It isn’t user-friendly if the content of destination anchors are covered by the sticky header. In my opinion that whole concept is not well done and definitely a bug. As a user and a multiple costumer of Enfold I expect that this works out-of-the-box. Please provide a sustainable solution within Enfold’s original code in an upcoming update.
What short-term solution can you offer?
Please see admin login in Private Content, if you need to enter my installation.Kind regards
UeliApril 4, 2022 at 8:30 am #1347099Hey Ueli,
Thank you for the inquiry.
Whqt happens when you use the color section element and apply the anchor or ID attribute (webcams) to it? Right now, the ID is applied directly to a text block instead of a color section.
Best regards,
IsmaelApril 15, 2022 at 6:41 pm #1348520Hi Ismael
I put the content into a color section, removed the ID “webcams” from the h2 title tag and applied it to the color section. The issue still consists.
I have just realized that on Enfold’s Support Website anchor IDs seem to work properly with the sticky header. See further details in Private Content.
I guess that Enfold’s Support Forum also runs with the Enfold Theme. So why does it work there but not on my website?
Kind regards
UeliApril 18, 2022 at 5:05 am #1348664Hi,
It is working correctly on our end now. This is where the page lands when we click the “webcams” link, or when we manually access the page using browser URL field.
Screenshot: https://1drv.ms/u/s!AjjTfXSRbKTvwFMEq-cWo4wuom7q?e=cJbr57
Please make sure to purge the cache or remove the browser history before checking the page.
Best regards,
IsmaelApril 20, 2022 at 1:01 am #1348873Hi Ismael
It’s definitely not working correctly! Of course, I always purge the cache or remove the browser history before checking the page.
Based on your screenshot, I assume you were using the Chrome browser and you were logged in the WordPress backend while testing the site. I’ve just found out if you do so, the issue seems gone. But if you are not logged in the backend while testing the site, the sticky header covers the title most of the time. However, if I repeat the test over and over again, it very rarely works. This observation applies to Chrome. With Firefox it doesn’t really matter if you are logged in the backend or not. Most of the times, the title stays covered.
So please test the site again without being logged in the backend.
Kind regards
UeliApril 25, 2022 at 3:36 am #1349315Hi,
I can reproduce the issue on Firefox but not on Chrome. Is it working correctly when you replace the two webtv iframes inside the webcams section with a default Video element?
Best regards,
IsmaelMay 7, 2022 at 8:38 pm #1350893Hi Ismael
I have completely removed the webcams to find out if it works at least with only title and text. Unfortunately it doesn’t. Both on Firefox and on Chrome the title is mostly covered by the header. I have tested on several windows desktop computers and even friends can reproduce the issue. It seems to work on iPad’s Safari.
What else can you do?
Kind regards
UeliMay 9, 2022 at 6:33 am #1350991Hi,
Thank you for your patience.
On Firefox, the scroll position is correct on initial load and when you do a hard refresh, which means that the script only fires correctly when you actually refresh the page. Firefox doesn’t seem to automatically refresh the page when you try to access it again by pressing the ENTER or RETURN key in the URL field. You have to do a hard refresh manually. On Chrome, this always works because the browser always refreshes the page automatically when you try to access the page again using the browser URL field. Scrolling also works correctly when you try to access the section using the anchor links at the very bottom of the page.
We modified the filter in the functions.php file a bit to adjust the offset on firefox browser but again this will only work on Firefox if the page is actually refreshed.
function avf_header_setting_filter_mod($header) { $offset = strpos($_SERVER['HTTP_USER_AGENT'], "Firefox") === false ? 50 : 75; if( ! wp_is_mobile() ) { $header['header_scroll_offset'] = $header['header_scroll_offset'] + $offset; } return $header; } add_filter('avf_header_setting_filter', 'avf_header_setting_filter_mod', 9999, 1);
Best regards,
IsmaelMay 13, 2022 at 5:03 pm #1351681Hi Ismael
I’m at a loss and a bit desperate.
I don’t understand why it should work well on your side but all of my colleagues (at least 4 people with different computers on different locations) report the same misbehavior that I’m talking about. If you click the “Webcams”-link on the homepage at a very first visit, the corresponding anchor title “Webcams” on the webpage “Aktuelles” stays covered by the sticky header. Please see new screen videos of Firefox and Chrome in Private Content. We always clear the cache and hard refresh the homepage before making the tests. The videos also show that if you go back and forth then the scrolling position is correct. But on an initial load (a very first visit) the offset script is not fired correctly.
Kind regards
UeliMay 15, 2022 at 2:27 pm #1351845Hi,
Thank you for your patience, perhaps this solution will help in your case, you can try this by adding this code to the end of your child theme functions.php file in Appearance ▸ Editor:function custom_offset_script() { ?> <script> (function($) { $('a[href*=#]:not([href=#])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); if (target.length) { $('html,body').animate({ scrollTop: target.offset().top - 125 //offsets for fixed header }, 1000); return false; } } }); //Executed on page load with URL containing an anchor tag. if($(location.href.split("#")[1])) { var target = $('#'+location.href.split("#")[1]); if (target.length) { $('html,body').animate({ scrollTop: target.offset().top - 125 //offset height of header here too. }, 1000); return false; } } }(jQuery)); </script> <?php } add_action('wp_footer', 'custom_offset_script');
I tested this on your page by injecting the code in my browser and it seemed to work well, instead of trying to land on the anchor while the page is still loading and adjusting it’s height, this script waits for the page to load and then scrolls to the anchor.
Best regards,
MikeMay 17, 2022 at 10:21 pm #1352130Hi Mike
Thanks! Your solution seems to work on all major browsers and I might live with that scrolling approach. But if the header isn’t sticky, like on smaller screens (width less than 768px), there must be a smaller offset. The code needs to be completed with a dependency of the screen width. For example, if the screen width is 768px or more then the offset is 80px and if the screen width is less than 768px then the offset is 10px. How can this be achieved?
Kind regards
UeliMay 18, 2022 at 1:56 am #1352134Hi,
Glad to hear, try this insteadfunction custom_offset_script_with_width() { ?> <script> (function($) { $('a[href*=#]:not([href=#])').click(function() { var width = $(window).width() if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); if (target.length) { if ($(window).width() < 768) { $('html,body').animate({ scrollTop: target.offset().top - 10 //offsets for fixed header }, 1000); return false; } else { $('html,body').animate({ scrollTop: target.offset().top - 80 //offsets for fixed header }, 1000); return false; } } } }); //Executed on page load with URL containing an anchor tag. if($(location.href.split("#")[1])) { var target = $('#'+location.href.split("#")[1]); if (target.length) { if ($(window).width() < 768) { $('html,body').animate({ scrollTop: target.offset().top - 10 //offset height of header here too. }, 1000); return false; } else { $('html,body').animate({ scrollTop: target.offset().top - 80 //offset height of header here too. }, 1000); return false; } } } }(jQuery)); </script> <?php } add_action('wp_footer', 'custom_offset_script_with_width');
Best regards,
MikeMay 20, 2022 at 5:41 pm #1352436Hi Mike
Your modification works well so far.
But I ran into several new issues because the scrolling script affects now every link/anchor with a # on my website. For example there is a link with a # that opens the lightbox containing a contact form. While the lightbox is opening in the foreground, the page scrolls back to the top in the background instead of staying in place.
I fixed these issues in completing your code with a if(is_page(PAGE ID)) conditional.
function custom_offset_script_with_width() { if(is_page(972)) { ?> <script> (function($) { $('a[href*=#]:not([href=#])').click(function() { var width = $(window).width() if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); if (target.length) { if ($(window).width() < 768) { $('html,body').animate({ scrollTop: target.offset().top - 10 //offset if header isn't sticky }, 1000); return false; } else { $('html,body').animate({ scrollTop: target.offset().top - 100 //offset if header is sticky }, 1000); return false; } } } }); //Executed on page load with URL containing an anchor tag. if($(location.href.split("#")[1])) { var target = $('#'+location.href.split("#")[1]); if (target.length) { if ($(window).width() < 768) { $('html,body').animate({ scrollTop: target.offset().top - 10 //offset if header isn't sticky here too. }, 1000); return false; } else { $('html,body').animate({ scrollTop: target.offset().top - 100 //offset if header is sticky here too. }, 1000); return false; } } } }(jQuery)); </script> <?php } } add_action('wp_footer', 'custom_offset_script_with_width');
The script runs now only on the specific page where it is needed. That works fine for me.
Another approach might be that your initial script affects only a specific anchor – #webcams in my case – instead of affecting every anchor with a #. How can this be achieved?
Kind regards
UeliMay 20, 2022 at 6:01 pm #1352441Hi,
Glad to hear, you could try editing('a[href*=#]:not([href=#])').click(function() {
to
('a[href*=#webcams]:not([href=#])').click(function() {
Best regards,
MikeMay 22, 2022 at 4:50 pm #1352542Hi Mike
That approach also works well. How does the code look like if I want to affect a second and third anchor – e. g. #beitraege and #berichte?
Kind regards
UeliMay 22, 2022 at 7:00 pm #1352549Hi,
You would need to copy the whole code for each link, it might be better to list your pages as an array like:
if(is_page(array(42, 57, 71))) {
Best regards,
MikeMay 27, 2022 at 12:53 pm #1353139Hi Mike
OK, I prefer the first approach while assigning the script to the pages where it is needed.
Thanks for your and Ismael’s help.Kind regards
UeliMay 27, 2022 at 1:06 pm #1353140Hi,
Glad we were able to help, if you have any further questions please create a new thread and we will gladly try to help you. Thank you for using Enfold.Best regards,
Mike -
AuthorPosts
- The topic ‘Sticky header covers content of destination anchor’ is closed to new replies.