0

I have a created a search system and have the screen scrolling down when the input box is clicked on only small size screens to give more focus on the input box. The scroll continues to work even if I resize the screen to out of the parameters. And visa versa (won't work if the screen started big). How do I make this work when the screen is resized.

Here is the code i have so far:

if (window.innerWidth < 700) { $(".sl-mobile").click(function () { $('html, body').animate({ scrollTop: $(".search-subheading").offset().top }, 2000); }) }else{ } 
1
  • 1
    I think your click handler should wrap the 700 width condition and not the other way around Commented Dec 4, 2018 at 20:56

1 Answer 1

1

Your code will bind the click event once when the script loads, if the window is smaller than 700. You should check the window width inside the click event instead, like this:

$(".sl-mobile").click(function () { if (window.innerWidth < 700) { $('html, body').animate({ scrollTop: $(".search-subheading").offset().top }, 2000); } }) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.