JavaScript: Events
Illustration by John Hersey

Dealing with layers


From:

JavaScript: Events

with Ray Villalobos

Video: Dealing with layers

There's a little bit of a problem with our application and it's sort of hard to debug. The problem is with CSS layering and CSS layering is controlled by the z index attribute. So, if I grab any one of these and I drag them over and I click on any other one you'll see that this new one is now underneath this one. And really, every element that I pick up should be on top of the previous element. Just in case I want to rearrange things at one point. So, this is sort of a small problem, but it's something we need to deal with. The reason that's happening is, if we Inspect Element right here is, because every one of these elements originally has a z-index of 5.

Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now
please wait ...
Watch the Online Video Course JavaScript: Events
2h 11m Intermediate Sep 17, 2013

Viewers: in countries Watching now:

Learn about the different types of JavaScript events available to use in your applications, including traditional events like mouse, form, and window events, plus events that allow you to manage the DOM and HTML5 events that deal with media. Author Ray Villalobos shows how to propagate and register events during the capturing and bubbling phases, cancel an event or default behavior, and respond to clicks, mouse-overs, right-clicks, following the mouse, and loading images. Plus, Ray demos an audio jukebox project and a drag-and-drop game that shows JavaScript desktop and touch events in action.

Topics include:
  • Understanding event registration and propagation
  • Analyzing event properties
  • Creating and removing DOM elements with events
  • Removing an event
  • Canceling and preventing default events
  • Playing media events
  • Handling media pauses
  • Dragging and dropping
  • Adding touch events
Subjects:
Developer Web
Software:
JavaScript
Author:
Ray Villalobos

Dealing with layers

There's a little bit of a problem with our application and it's sort of hard to debug. The problem is with CSS layering and CSS layering is controlled by the z index attribute. So, if I grab any one of these and I drag them over and I click on any other one you'll see that this new one is now underneath this one. And really, every element that I pick up should be on top of the previous element. Just in case I want to rearrange things at one point. So, this is sort of a small problem, but it's something we need to deal with. The reason that's happening is, if we Inspect Element right here is, because every one of these elements originally has a z-index of 5.

As soon as we start dragging them they get a Z index of 10. But because I drag these two elements, they both have the same Z index. And that's a problem because the one I dragged first is going to be on top of the one I dragged second. So in order to make sure our objects are always on top, we're going to have to write a function and fix that. So I'm going to create a function up here called reset Z. So I'll say function. And we'll call it reset z. And this function is just going to look for every element that is an image in the dom.

So, I'll sign that to a variable called elements and I'll use query selector. This time I'm going to use query selector all. Because that's going to return an array of all the elements matching whatever I have in here. So I'm going to look for all the IMG elements and then I'll create a for loop and the way that this works is I want to go through all the elements. And I want to set this style of all the elements that I get returned by that and grab the z index of every one of those elements and set them back to 5.

So this is just going to take every single image and reset them all to have an index of 5. And so what I want to do is, right before I set the z index of the element that I clicked on. I want to call this function, to make sure that every other element has a z index of 5. You may want to note that whenever I do any CSS property, I don't use the name of the property right here when I'm using this method of calling it. So instead of using z-index, I have to use camel casing, so the Index here is capitalized.

So let me save that, and then I'm going to go over here and refresh, and I'm going to grab this. And then I'm going to drag this one, and this one should be on top when I do it. So let me just go ahead and right click on inspect element. And sure enough, this one right now has an index of ten because I just grabbed it. And then when I click on this one keep your eye down on the Elements panel. When I grab this one, now that one should have the z-index of 10. So let's see, this one now, get the z-index of 10, you can see it right here.

And when I click on this one, that one's going to go to the top. And now that one has a z-index of 10. So now everything is layered properly. It's not a really big problem, but it can really get annoying with working with some elements on the page. Once you're done with your main code, it's a good idea to look for small issues like this. That will improve the user experience of your application.

There are currently no FAQs about JavaScript: Events.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed JavaScript: Events.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

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.