-
AuthorPosts
-
January 24, 2015 at 10:09 pm #385005
On pages with little content, my socket becomes very big. Because Enfold currently has no sticky footer/socket functionality, i’m trying to set the height dynamically using browser viewport height and jQuery.
Basic idea:
1. determine browsers viewport height
2. subtract the footer height and header height (main_content_height = browser_viewport_height – header_height – footer_height)
3. set the calculated heightThe CSS code I want to use:
#top .main_color.container_wrap.fullsize{ min-height: [value]px; }
My problem is that I have no experience using jQuery, so I don’t know how to do this. Using Google, i’ve been able to figure out the following.
In the functions.php of enfold-child I added:
function load_my_scripts() { wp_register_script('myscript', get_stylesheet_directory_uri().'/js/myscript.js', false, '1.0', true ); wp_enqueue_script('myscript'); } add_action('init', 'load_my_scripts')
Then i created “/wp-content/themes/enfold-child/js/myscript.js”. And in order to valide the script is actually loaded and executed, I added the following code:
window.onload = function() { alert( "testing123" ); };
Now, when refreshing a page, an alert box is show with the text “testing 123”. The script is working. So far so good. But now I have no idea howto actually code the previosly mentioned steps in jQuery. I’ve tried the following, but that’s not working:
window.onload = function() { $('#top .main_color.container_wrap.fullsize') .css({'height': (($(window).height()) - 125 - 51)+'px'}); };
Any idea’s?
January 26, 2015 at 12:36 pm #385389Hi dennisbaaten!
Thank you for using Enfold.
Try this on functions.php:
add_filter('wp_footer', 'avf_socket_height', 10); function avf_socket_height() { ?> <script> (function($){ $(window).load(function(){ var headerh = $("#header").height(), footerh = $("#footer").height(), windowh = $(window).height(), socketh = windowh - footerh - headerh; $('#socket').css("height", socketh + 'px') }); })(jQuery); </script> <?php }
Remove browser cache then reload the page.
Best regards,
IsmaelJanuary 27, 2015 at 12:16 pm #386151Hi Ismael,
Thank you for your reply.
When using your code, my socket becomes even bigger. That’s because you are setting the height of the socket, while the idea is to set the heigt of the main window. I think it should be something like this:add_filter('wp_footer', 'avf_socket_height', 10); function avf_socket_height() { ?> <script> (function($){ $(window).load(function(){ var headerh = $("#header").height(), footerh = $("#footer").height(), windowh = $(window).height(), socketh = $("#socket").height(), mainh = windowsh - footerh - headerh - socketh; $('#top .main_color.container_wrap.fullsize').css("height", mainh + 'px') }); })(jQuery); </script> <?php }
This code however has no effect. I’ve also tried to set the height manually to for example 1000 px (.css(“height”, 1000 + ‘px’) ) just to check if there is any change. But this is also not working. When setting the same CSS identifier and selector to 1000 px in the Child theme’s style.css it is working correct. So there is still something wrong with the way i am trying to set the height from the jQuery script.
January 28, 2015 at 9:55 am #386842Hey!
The windowsh should be windowh.
add_filter('wp_footer', 'avf_socket_height', 10); function avf_socket_height() { ?> <script> (function($){ $(window).load(function(){ var headerh = $("#header").height(), footerh = $("#footer").height(), windowh = $(window).height(), socketh = $("#socket").height(), mainh = windowh - footerh - headerh - socketh; $('#top .main_color.container_wrap.fullsize').css("height", mainh + 'px') }); })(jQuery); </script> <?php }
Best regards,
IsmaelJanuary 30, 2015 at 9:35 am #388159Hi Ismael,
Getting closer… :-)
With your help I’ve been able to make some changes to the script because:
– I’m using a 250px height layerslider on one of my pages.
– I don’t only want this to work on page load, but also on window resizeMy current code:
add_filter('wp_footer', 'avf_socket_height', 10); function avf_socket_height() { ?> <script> (function($){ $(window).load(function(){ var headerh = $("#header").height(), footerh = $("#footer").height(), windowh = $(window).height(), socketh = $("#socket").height(), wpadminbarh = $("#wpadminbar").height(), layersliderh = $("#layer_slider_1").height(), afterlayersliderh = $("#after_layer_slider_1").height(); if ($("#layer_slider_1").length > 0) { mainh = (windowh - footerh - headerh - socketh - wpadminbarh - 250 - 1) / 2; $('#top .main_color.container_wrap.fullsize').css("height", mainh + 'px'); $('#layer_slider_1').css("height", 250 + 'px'); } else { mainh = windowh - footerh - headerh - socketh - wpadminbarh; $('#top .main_color.container_wrap.fullsize').css("height", mainh + 'px'); } }); $(window).resize(function(){ var headerh = $("#header").height(), footerh = $("#footer").height(), windowh = $(window).height(), socketh = $("#socket").height(), wpadminbarh = $("#wpadminbar").height(), layersliderh = $("#layer_slider_1").height(), afterlayersliderh = $("#after_layer_slider_1").height(); if ($("#layer_slider_1").length > 0) { mainh = (windowh - footerh - headerh - socketh - wpadminbarh - 250 - 1) / 2; $('#top .main_color.container_wrap.fullsize').css("height", mainh + 'px'); $('#layer_slider_1').css("height", 250 + 'px'); } else { mainh = windowh - footerh - headerh - socketh - wpadminbarh; $('#top .main_color.container_wrap.fullsize').css("height", mainh + 'px'); } }); })(jQuery); </script> <?php }
As you can see it was quite the puzzle to figure this out when using layerslider. Layerslider adds two extra DIVs (so 3 in total) to the main part of the page: layer_slider_1 and after_layer_slider_1. When only setting the height of “#top .main_color.container_wrap.fullsize”, this height is applied to all the 3 div’s and then the main part becomes 3 times the height I need it to be. In order to work around this problem, I’ve set the height of the layer_slider_1 div manually (although you specifiy the size in the layerslider pluging, it’s not applied by default), and divided the remaining part by 2 so “#top .main_color.container_wrap.fullsize” and “after_layer_slider_1” are equal.
Although this seems to be working for the moment, this doesn’t seem a nice and futureproof solution. Any idea’s on howto improve this code further?
- This reply was modified 9 years, 9 months ago by dennisbaaten.
February 1, 2015 at 8:52 am #388967Hi!
It works fine on my installation. Shorten the code with this:
add_filter('wp_footer', 'avf_socket_height', 10); function avf_socket_height() { ?> <script> (function($){ $(window).resize(function(){ var headerh = $("#header").height(), footerh = $("#footer").height(), windowh = $(window).height(), socketh = $("#socket").height(), wpadminbarh = $("#wpadminbar").height(), layersliderh = $("#layer_slider_1").height(), afterlayersliderh = $("#after_layer_slider_1").height(); if ($("#layer_slider_1").length > 0) { mainh = (windowh - footerh - headerh - socketh - wpadminbarh - 250 - 1) / 2; $('#top .main_color.container_wrap.fullsize').css("height", mainh + 'px'); $('#layer_slider_1').css("height", 250 + 'px'); } else { mainh = windowh - footerh - headerh - socketh - wpadminbarh; $('#top .main_color.container_wrap.fullsize').css("height", mainh + 'px'); } }).resize(); })(jQuery); </script> <?php }
Cheers!
IsmaelDecember 9, 2020 at 12:24 pm #1265968After upgrading to WordPress 5.6 this solution stopped working. I’ve run a quick test, and it seems that the code within the function “avf_socket_height” is not executed at all.
Any suggestions?December 9, 2020 at 1:52 pm #1265995I’ve been messing around with this piece of code, and made some improvements. Notice that I’m not using LayerSlider as I did before, so I removed this part of the height calculation.
add_action('wp_footer', 'avf_socket_height', 10); function avf_socket_height() { ?> <script> ( function($) { $(window).load(function() { var headerh = $("#header_main").height(); var windowh = $(window).height(); var socketh = $("#socket").height(); if ($("#wpadminbar").length > 0) { var wpadminbarh = $("#wpadminbar").height(); } else { var wpadminbarh = 0; } var mainh = windowh - headerh - socketh - wpadminbarh; $('#main .main_color').css("height", mainh + 'px'); }); $(window).resize(function() { var headerh = $("#header_main").height(); var windowh = $(window).height(); var socketh = $("#socket").height(); if ($("#wpadminbar").length > 0) { var wpadminbarh = $("#wpadminbar").height(); } else { var wpadminbarh = 0; } var mainh = windowh - headerh - socketh - wpadminbarh; $('#main .main_color').css("height", mainh + 'px'); }); } )(jQuery); </script> <?php }
December 10, 2020 at 6:29 am #1266180 -
AuthorPosts
- You must be logged in to reply to this topic.