-
AuthorPosts
-
April 8, 2020 at 1:37 pm #1201908
Hi!
I am trying to move a sticky logo and sticky header widget behind the main content of the page when scrolling down. The content needs to move over the logo and the widget, but under the
#header_main
. In the private section you see the URL and it becomes clear what I mean.What I have done: put a background image in the
div#header_main
which is the same as thehtml
background, so the content of#main
scrolls under thediv#header_main
of the page. I have tried to give the logo and the header widget a different z-index and positioning, but it seems that this is not working.Can you help me to move a sticky logo and sticky header widget behind the main content of the page when scrolling down, but under the
#header_main
?Thanks!
April 15, 2020 at 2:00 pm #1203982Hey Daniel,
Sorry for the late reply!
Child elements would inherit z-index value from their parent so using only CSS that would not be easily possible. You could use jQuery to hide logo and header widget when scrolled down. Please try adding following code to bottom of functions.php file in Appearance > Editor
function add_hide_logo(){ ?> <script> jQuery(window).scroll(function(){ if(jQuery(this).scrollTop() > 200) { jQuery('span.logo').addClass('hidden'); } else { jQuery('span.logo').removeClass('hidden'); } }); </script> <?php } add_action('wp_footer', 'add_hide_logo');
Best regards,
YigitApril 16, 2020 at 1:08 pm #1204411Hey Yigit, thanks for thinking along with me! Too bad it isn’t easily done with z-index.
The jQuery you are suggesting will hide these elements, but they should not be hidden, only put behind all the content in
#main
. Do you have any good suggestions for that?Kind regards, Daniel
April 17, 2020 at 10:54 am #1204689Hi Daniel,
I am afraid, I do not however it is possible to add fade in/fade out to transition so it would slowly fade out when scrolled down. Would that help?
Best regards,
YigitApril 20, 2020 at 3:30 pm #1205507Hey Yigit,
That was my first solution, to let them fade out when scrolling down. Unfortunately the designer is demanding these two elements to remain visible all the time, but to go under the elements in the
#main
section. Would there be another solution, maybe not to use the logo and header widget but something alternatively?Thanks for helping! Kind regards,
DanielApril 26, 2020 at 9:13 pm #1207354Hi,
Sorry for the late reply, this function will move the header widget, logo, and the search after the #main and applies z-index so they are fixed and under the content scroll.
Try adding this code to the end of your functions.php file in Appearance > Editor:function custom_script(){ ?> <script> (function($){ $(document).ready(function(){ $( '#wrap_all' ).each(function() { $( this ).find( 'span.logo' ).insertAfter( $(this).find('div#main') ); $("span.logo").css({ 'position': 'fixed','z-index': '0' }); $( this ).find( 'div#search-2' ).insertAfter( $(this).find('div#main') ); $("div#search-2").css({ 'position': 'fixed','z-index': '0','right': '10em' }); $( this ).find( 'div#custom_html-3' ).insertAfter( $(this).find('div#main') ); $("div#custom_html-3").css({ 'position': 'fixed','z-index': '0','right': '10em' }); }); }); })(jQuery); </script> <?php } add_action('wp_footer', 'custom_script');
and then add this css in the General Styling > Quick CSS field or in the WordPress > Customize > Additional CSS field:
#top #wrap_all #search-2 { top: 70px !important; } #top #custom_html-3 { top: 40px !important; } #top #custom_html-3 .custom-html-widget { height: 238px; width: 200px; }
Then clear your browser cache and check.
Please see the screenshot in Private Content area.Best regards,
MikeApril 27, 2020 at 9:57 pm #1207623Hi @mike,
Thanks so much for helping out! This script + css do indeed put the elements behind the
#main
content which is exactly what I want. They layout is now broken unfortunately, because of theright: 10em
used. And also on mobile devices the elements are disappeared completely.1) How can we change the script so it becomes responsive? I changed 10em to 11% but that’s only valid until 1400px. From 990px to 1400px it should be
right: 50px
(i.e. it should align right with the main menu).2) The z-index of the two elements should be 501 when the viewport is under 990px. How do I change this in the script?
Thanks again!
DanielApril 28, 2020 at 11:28 am #1207751Hi,
Thank you for the feedback, please remove the function above, I will re-write it with the 990 brake point and remove the css so it can be adjusted with the other css.
You can use media queries in your css to adjust for certain screen sizes,@media only screen and (min-width: 990px) and (max-width: 1400px) { .your { css: here; } }
Best regards,
MikeApril 28, 2020 at 5:34 pm #1207875April 29, 2020 at 12:22 pm #1208127Hi,
It seems like the function above is still active, please remove it and the css so I can write and test the new script.Best regards,
MikeApril 29, 2020 at 12:23 pm #1208128April 30, 2020 at 1:30 pm #1208503Hi,
Thank you, please also look in your child theme style.css for:#search-2, div.avia-search-tooltip { top: 30px !important; left: -30px; }
and change to:
div.avia-search-tooltip { top: 30px !important; left: -30px; }
Then add this css:
@media only screen and (min-width: 989px) { #top #wrap_all #search-2 { top: 70px !important; right: 12%; } #top #custom_html-3 { top: 40px !important; right: 12%; } #top #custom_html-3 .custom-html-widget { height: 238px; width: 200px; } } @media only screen and (max-width: 988px) { #top #wrap_all #search-2 { top: 30px !important; right: 20px; } #top #custom_html-3 { top: 40px !important; right: 2%; } #top #custom_html-3 .custom-html-widget { height: 238px; width: 200px; } } @media only screen and (max-width: 767px) { .responsive #top .logo { top: 0 !important; } .logo a { top: 0px !important; } #top #wrap_all #custom_html-3 { top: 0px !important; } }
and this function in your functions.php:
function custom_script(){ ?> <script> (function($){ $(document).ready(function(){ var width = $(window).width() if ((width >= 989)) { $( '#wrap_all' ).each(function() { $( this ).find( 'span.logo' ).insertAfter( $(this).find('div#main') ); $("span.logo").css({ 'position': 'fixed','z-index': '0' }); $( this ).find( 'div#search-2' ).insertAfter( $(this).find('div#main') ); $("div#search-2").css({ 'position': 'fixed','z-index': '0' }); $( this ).find( 'div#custom_html-3' ).insertAfter( $(this).find('div#main') ); $("div#custom_html-3").css({ 'position': 'fixed','z-index': '0' }); }); } else { $( '#wrap_all' ).each(function() { $( this ).find( 'span.logo' ).insertAfter( $(this).find('div#main') ); $("span.logo").css({ 'position': 'fixed','z-index': '501' }); $( this ).find( 'div#search-2' ).insertAfter( $(this).find('div#main') ); $("div#search-2").css({ 'position': 'fixed','z-index': '501' }); $( this ).find( 'div#custom_html-3' ).insertAfter( $(this).find('div#main') ); $("div#custom_html-3").css({ 'position': 'fixed','z-index': '501' }); }); } }); })(jQuery); </script> <?php } add_action('wp_footer', 'custom_script');
Then clear your browser cache and check.
Best regards,
MikeMay 11, 2020 at 11:43 am #1211805Hi @mike,
Thanks again, I think we are getting closer to the desired end result, which is great! :)
I made some small changes to your code to improve the visual experience. I also got approval from the designer to remove the fixed header on mobile. This gives me two – I hope final – questions:
1) I have changed the function so the elements have an absolute position in a mobile view, see below:
function custom_script(){ ?> <script> (function($){ $(document).ready(function(){ var width = $(window).width() if ((width >= 989)) { $( '#wrap_all' ).each(function() { $( this ).find( 'span.logo' ).insertAfter( $(this).find('div#main') ); $("span.logo").css({ 'position': 'fixed','z-index': '0' }); $( this ).find( 'div#search-2' ).insertAfter( $(this).find('div#main') ); $("div#search-2").css({ 'position': 'fixed','z-index': '0' }); $( this ).find( 'div#custom_html-3' ).insertAfter( $(this).find('div#main') ); $("div#custom_html-3").css({ 'position': 'fixed','z-index': '0' }); }); } else { $( '#wrap_all' ).each(function() { $( this ).find( 'span.logo' ).insertAfter( $(this).find('div#main') ); $("span.logo").css({ 'position': 'absolute','z-index': '501' }); $( this ).find( 'div#search-2' ).insertAfter( $(this).find('div#main') ); $("div#search-2").css({ 'position': 'absolute','z-index': '501' }); $( this ).find( 'div#custom_html-3' ).insertAfter( $(this).find('div#main') ); $("div#custom_html-3").css({ 'position': 'absolute','z-index': '501' }); }); } }); })(jQuery); </script> <?php } add_action('wp_footer', 'custom_script');
When I make my browser width smaller, the elements remain fixed in a mobile view: https://daan.me/up/Screenshot%202020-05-11%20at%2011.31.56.png Is there away to avoid this?
2) When I open the mobile menu, the items are visible in the menu: https://daan.me/up/Screenshot%202020-05-11%20at%2011.42.05.png They should not be visible when the menu is opened. Can we do this?
Thanks again for your help!
Regards, Daniel
May 11, 2020 at 1:41 pm #1211834Hi,
When testing the elements in the developer tools for mobile, please try refreshing the page first, this corrects the fixed items behind the header. This occurs because the browser doesn’t recognize the change in the window width on manual resize.
In the real world a user’s screen size doesn’t change from desktop to mobile without a fresh page load.
To change the visibility of your header elements when the burger menu is clicked try adding this code to the end of your functions.php file in Appearance > Editor:function custom_visibility_script(){ ?> <script> (function($){ $(document).ready(function(){ var width = $(window).width() if ((width <= 768)) { $( '.menu-item-avia-special' ).click(function(){ $("span.logo").css({'visibility': 'hidden' }); $("div#search-2").css({'visibility': 'hidden' }); $("div#custom_html-3").css({'visibility': 'hidden' }); }); } else { $("span.logo").css({'visibility': 'visible' }); $("div#search-2").css({'visibility': 'visible' }); $("div#custom_html-3").css({'visibility': 'visible' }); } }); })(jQuery); </script> <?php } add_action('wp_footer', 'custom_visibility_script');
Best regards,
MikeDecember 4, 2020 at 12:45 pm #1264986Hi @mike,
getting back on an older thread. I have added another widget (with id
#custom_html-4
) to the header and I want to give this one mainly the same behaviour as#custom_html-3
. When I change my functions.php and copy the#custom_html-3
lines for#custom_html-4
, my widget disappears.What can I do to make this work? The link is in the private section. One difference with
#custom_html-3
is that#custom_html-4
must be hidden under 990px.edit: my primary goal is to have it hidden under the main content when scrolling, but z-index isn’t working.
Can you help me out?
Thanks!
Daniel- This reply was modified 4 years ago by Daniel.
December 4, 2020 at 1:42 pm #1265003Hi,
As I understand you would like the content to scroll over the header and menu & widgets, please try this css:#header { z-index: 0 !important; }
After applying the css, please clear your browser cache and check.
Best regards,
MikeDecember 4, 2020 at 2:40 pm #1265019Hi @mike,
No, the content scrolls under the header, but over the logo and widgets in the header area. If you scroll down, you will see that the header remains visible and the main content scrolls under the header. The header_main that contains the main menu, stays on top. The other elements in the header section, such as logo, search and custom-html widgets, go behind the main content when scrolling.
Regards, Daniel
December 5, 2020 at 2:26 pm #1265180Hi,
Thank you for the feedback, so the issue is the “Corona maatregelen” box in #custom_html-4 being on top of the content on scroll.
The #header z-index of 501 is affecting the widget even if it is set to zero. I believe this is because the #custom_html-4 is in the inner-container div, where the #custom_html-3 widget is in the #wrap_all div above the #header div, so the z-index of 501 doesn’t affect it.
Are you using the same hook for the #custom_html-4 widget area as you did for #custom_html-3?
Can I login and fiddle with it and the script?
So the goal is to show this box above 767px, but on scroll it needs to be behind the content?
If we can’t put #custom_html-4 in the #wrap_all div, or sort out the z-index, can we just hide it on scroll?Best regards,
MikeDecember 6, 2020 at 10:53 am #1265263Hi Mike,
Hiding it on scroll is probably even the better option! I didn’t want to ask, because I thought it would be easier to edit the existing code.
Daniel
December 6, 2020 at 2:07 pm #1265271Hi,
Ok, please try this script to hide the box on scroll, try adding this code to the end of your functions.php file in Appearance > Editor:function custom_messagebox_script() { ?> <script> (function ($) { $(window).scroll(function() { var scroll = $(window).scrollTop(); if (scroll >= 20) { $("#avia-messagebox-").hide(); } else { $("#avia-messagebox-").show(); } }); })(jQuery); </script> <?php } add_action('wp_footer', 'custom_messagebox_script');
Then clear your browser cache and check.
Best regards,
MikeDecember 7, 2020 at 12:40 pm #1265458Hi Mike, thanks for that! I altered the code somewhat to have it fade away on scroll. I changed:
$("#avia-messagebox-").hide();
to
$("#avia-messagebox-").stop().fadeTo('fast', 0);
Full script:
// Hide messagebox on scroll function custom_messagebox_script() { ?> <script> (function ($) { $(window).scroll(function() { var scroll = $(window).scrollTop(); if (scroll >= 20) { $("#avia-messagebox-").stop().fadeTo('fast', 0); } else { $("#avia-messagebox-").stop().fadeTo('slow', 1); } }); })(jQuery); </script> <?php } add_action('wp_footer', 'custom_messagebox_script');
Regards, Daniel
- This reply was modified 4 years ago by Daniel.
December 7, 2020 at 1:28 pm #1265471Hi,
Thank you for sharing your solution, the fadeTo is nicer :)
Unless there is anything else we can assist with on this issue, shall we close this then?Best regards,
MikeDecember 17, 2020 at 10:53 am #1268067Hi @Mike,
An issue that we have now, is that the #custom_html-4 is still somehow ‘active’ after fading out. This means that links cannot be clicked when they are in the area where the widget is:
edit: for now, I have replaced the fade effect with your initial hide effect, to make it work. I would prefer to have the fade effect if that is still possible though.
FADE effect result:
How can we fix this?
Thanks! Daniel
- This reply was modified 4 years ago by Daniel.
December 20, 2020 at 1:04 am #1268697Hi,
Sorry for the late reply, please try changing the z-index of the element like this:(function ($) { $(window).scroll(function() { var scroll = $(window).scrollTop(); if (scroll >= 20) { $("#avia-messagebox-").stop().fadeTo('fast', 0).css({'z-index':'0'}); } else { $("#avia-messagebox-").stop().fadeTo('slow', 1).css({'z-index':'10'}); } }); })(jQuery);
I’m not sure that the z-index of “10” is correct for when it is in view, so please try adjusting this to suit.
If this doesn’t help, please include an admin login in the private content area so I can test.Best regards,
Mike -
AuthorPosts
- You must be logged in to reply to this topic.