Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Toggling visibility of HTML elements upon reaching(while scrolling) a section or id?

Featured Replies

Hi,

 

Is it possible for an element, say a div or img, to be visible only if a specified section or id on the webpage was on screen.

 

How can I achieve this?

 

Thanks,

R

Yes, you can do this with JavaScript / jQuery.

 

You could create a function that shows an element when the scrollbar is at a certain position.

 

Got an example of what you want?

  • Author

Yeah. I've been trying to do this.

 

And can this be done using CSS rather than Javascript or jQuery?

 

Thanks

post-66007-0-01638900-1435397891_thumb.jpg

You can get the offset().top value of the support section, then compare that with the current scroll position plus the window height. Make this comparison on the scroll event then once the values overlap then add a class to the element and do as you wish in your CSS.

var isElementInViewport = function() {
  var scrollTop = $(window).scrollTop(),
      winHeight = $(window).height(),
      element = $('#supportSection'),
      elementOffset = element.offset().top;
 
  if (elementOffset < scrollTop + winHeight) {
    element.addClass('fade-in');
  }
  
}
$(window).scroll(isElementInViewport);

Beware though of attaching functionality to scroll events, they can fire hundreds of times a second and if there's any complexity there or DOM interactions your scroll performance will suffer. It might be worth looking into scroll throttling if you find this to be the case.

Edited by rbrtsmith

There might be an alternative sollution.

There is a method called getBoundingClientRect()

Apparently you can use it to return if an element is within a defined bounding box. If you set the bottom- and right-propperties of the bounding box to the clientHeight/clientWidth you can detect if the element is in the viewport or not without any fixed values.

See how to create the function here

http://stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport/7557433#7557433

Depending on what the function returns you can toggle between visible and a hidden class for the other element.

 

Like rbrtsmith said scroll events can be resource intensive so if you don't mind a small delay you could call the function with a timer event every 500 ms or so. se setInterval();

Edited by Nillervision

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.