0

I have the following code:-

if(jQuery(window).width() <= 992) {

    $('#menu-main-menu').prepend('<a id="book-now-menu" href="https://bookings.planetbouncetrampolinepark.com/JumpBookings/BookSession.aspx?site=1&group=1">BOOK NOW</a>');

    var social_menu =  '<div id="social-menu">';
        social_menu += '<a href="" target="_blank"><span class="icon-social icon-facebook"></span></a>';
        social_menu += '<a href="" target="_blank"><span class="icon-social icon-instagram"></span></a>';
        social_menu += '<a href="" target="_blank"><span class="icon-social icon-twitter"></span></a>';
        social_menu += '<a href="" target="_blank"><span class="icon-social icon-youtube"></span></a>';
        social_menu += '</div>';

    $('#menu-main-menu').append(social_menu);

}

This works fine when you refresh the page at below 992px or greater than than 992px. But in terms of responsiveness, it will either attach the code based on what your initial browser width is. Is there a way to get round this so that it detects the browser width on change and will either add/remove the above code?

Thanks in advance.

1
  • Why don't you added it always to your page and control the visibility using css media queries? Commented Feb 26, 2016 at 13:37

2 Answers 2

1

Why dont you use:

@media all(max-width: 992){
.social_menu{
display: none; 
}

That way you take up less browser ressources on calculating on every resize..

Sign up to request clarification or add additional context in comments.

1 Comment

Don't know why I didn't think if doing this. I basically removed if(jQuery(window).width() <= 992) { so it appends the content regardless, then added .social_menu { display: none; } , and used media queries as above to display them for mobile devices. Thanks
0

I think listener for a $(window).resize(function(){...}) should solve your problem. Just insert your code inside it, and function will apply every time your resize the window.

1 Comment

I have added this and added an else clause; jQuery('#book-now-menu, #social-menu').remove(); - but say you change the browser size from 800px to 500px, it is adding 6 rows of the social-menu, is there a way to only trigger the change once when it goes below or greater than 992px?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.