New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Adjusting for scrolling

From: JavaScript: Enhancing the DOM

Video: Adjusting for scrolling

So our page is looking really good. If we click on one of these images, everything looks great. Centered on the screen, top of the overlay. I can click on it to make it go away. But there's a little bit of a problem. If I click on one of these and I scroll the window. You can see that the overlay doesn't move with the scroll. And that looks a little funky so we're going to fix that next. Let me go back into my code. And right here I'm going to listen for a window event when somebody scrolls the window. So I'll say window, addEventListener, I'm looking for a scroll event.

Adjusting for scrolling

So our page is looking really good. If we click on one of these images, everything looks great. Centered on the screen, top of the overlay. I can click on it to make it go away. But there's a little bit of a problem. If I click on one of these and I scroll the window. You can see that the overlay doesn't move with the scroll. And that looks a little funky so we're going to fix that next. Let me go back into my code. And right here I'm going to listen for a window event when somebody scrolls the window. So I'll say window, addEventListener, I'm looking for a scroll event.

And once that happens I'm going to execute a function. And I'm going to return false so that it bowls properly. And in here I'm going to first make sure that I have the element called myOverlay. Because I don't really want to adjust anything unless that element exists. So I'll say, myOverlay style top. And I'll set it to the window. Page YOffset plus pixels because this is CSS.

And then do the same thing for the overlay style left window page XOffset plus pixels. So let's save that and see if it's working. Back into our page, refresh the page, click on one of these elements and try scrolling the window. And now you can see that that moves with the scroll of the page. And let me go ahead and click that out. So sometimes you really have to pay attention to these kind of details by really messing around with the structure of the page to make sure you catch anything that may cause the user some grief.

Show transcript

This video is part of

Image for JavaScript: Enhancing the DOM
JavaScript: Enhancing the DOM

36 video lessons · 7363 viewers

Ray Villalobos
Author

 
Expand all | Collapse all
  1. 2m 36s
    1. Welcome
      59s
    2. What you should know before watching this course
      50s
    3. Using the exercise files
      47s
  2. 24m 33s
    1. What is the Document Object Model (DOM)?
      3m 2s
    2. Navigating the DOM with developer tools
      8m 10s
    3. Testing JavaScript commands with the console
      5m 50s
    4. Communicating with the console through JavaScript
      7m 31s
  3. 31m 9s
    1. Selecting elements with getElementById
      4m 10s
    2. Choosing elements by HTML tag
      3m 20s
    3. Isolating elements by class name
      3m 12s
    4. Querying CSS to select elements
      4m 54s
    5. Working with named form elements
      3m 39s
    6. Understanding nodeType, nodeName, and nodeValue
      4m 30s
    7. Traversing up and down DOM nodes
      4m 40s
    8. Targeting node elements
      2m 44s
  4. 22m 25s
    1. Changing HTML attributes
      5m 25s
    2. Working with restricted attributes
      2m 49s
    3. Detecting data attributes
      3m 29s
    4. Controlling classes with the HTML5 classList
      3m 21s
    5. Targeting the attributes property
      1m 24s
    6. Using text content modifiers
      3m 42s
    7. Modifying elements as text
      2m 15s
  5. 14m 57s
    1. Creating and appending nodes
      4m 27s
    2. Controlling node insertions with insertBefore
      3m 17s
    3. Cloning and removing nodes
      4m 41s
    4. Replacing existing nodes
      2m 32s
  6. 26m 14s
    1. What we'll build
      2m 16s
    2. Adding a bubbling event listener
      4m 11s
    3. Creating and styling an overlay with JavaScript
      4m 39s
    4. Adding an image
      3m 48s
    5. Resizing images in the DOM
      2m 59s
    6. Centering an image
      2m 36s
    7. Handling clicks
      1m 29s
    8. Adjusting for scrolling
      1m 36s
    9. Detecting and adjusting for a window resize
      2m 40s
  7. 1m 49s
    1. Next steps
      1m 49s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.