Skip to content
the poor coder

$(window).scrolltop() not working on mobile

Poor Man
Published
Reading time
1 min read

Why is $(window).scrollTop() not working on mobile?#

If you are a developer, you may have come across the issue where the $(window).scrollTop() function does not work on mobile devices. This can be frustrating, especially if you are trying to create a responsive website that works on all devices.

One of the reasons for this issue is that mobile devices have a different way of handling scrolling than desktop devices. On a desktop device, you can scroll by using the mouse or trackpad, or by clicking on the scroll bar. However, on mobile devices, scrolling is done by swiping with a finger or tapping on the screen.

Possible solutions#

There are a few ways to solve this issue:

  • Use $(document).scrollTop() instead: This function works on both desktop and mobile devices. It returns the current vertical position of the scroll bar for the entire document.
  • Use a plugin: There are several plugins available that can help you handle scrolling on mobile devices. One such plugin is TouchSwipe, which allows you to handle swipes and taps on mobile devices.
  • Check for touch events: You can check if the device supports touch events using the following code:
Text

if ('ontouchstart' in window) {
  // This device supports touch events
} else {
  // This device does not support touch events
}

By using one of these solutions, you can ensure that your website works correctly on both desktop and mobile devices.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC