detinu20
detinu20

Reputation: 319

Window resizing and scroll

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{

}

Upvotes: 0

Views: 38

Answers (1)

jmcgriz
jmcgriz

Reputation: 3358

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);
  }
})

Upvotes: 1

Related Questions