skyleguy
skyleguy

Reputation: 1161

General Problems with Hover Transitions

I am having some trouble with hover transitions. I have a fullwidth bar of 13 images. When you hover over them, some text appears depending on which one you hover over. I would like to slow down the transition time, and also make it so the images go opaque at the same time the image slides up (currently it slides up and then goes opaque). And if at all possible, it would be neat to have an image stay "hovered" until one of the other images is hovered. One more thing, it gets a little jittery if you hover near the bottom of an image and move the cursor left or right, hopefully this can be fixed too.

What I have so far is on this page: http://homeinspectioncarync.com/testpage/

Following is the CSS and HTML I've used. Sorry for the one-line HTML, I couldn't quite figure out how to format that correctly in here. It follows the same pattern though img1 /img p1 /p img2 /img p2 /p and so on until it gets to 13. I know the community here is great so any help is appreciated!

CSS:

.images {
  height: 100px;
  width: 100px;
  position: absolute;
  -webkit-transition: all 1000ms ease;
  -moz-transition: all 1000ms ease;
  -ms-transition: all 1000ms ease;
  -o-transition: all 1000ms ease;
  transition: all 1000ms ease;
}

.images:hover {
  opacity: 0.5;
  top: -10px;
}

.para {
  position: absolute;
  left: 600px;
  top: 200px;
  font-size: 200%;
  visibility: hidden;
}

#one:hover + #pone {
  visibility: visible;
}

#two:hover + #ptwo {
  visibility: visible;
}

#three:hover + #pthree {
  visibility: visible;
}

etc. (until 13)

HTML:

<img src = "https://media.licdn.com/mpr/mpr/shrinknp_200_200/p/3/000/00c/2ab/35ae340.jpg" class = "images" id = "one"></img>
<p class = "para" id = "pone">1</p>
<img src = "http://homeinspectionraleighnc.com/wp-content/uploads/2010/11/Glenns-Photo-1.png" class = "images" id = "two" style = "left: 100px;"></img>
<p class = "para" id = "ptwo">2</p>
<img src = "https://scontent-sjc2-1.xx.fbcdn.net/v/t1.0-1/p160x160/18718_2753855091076_8128390731521222770_n.jpg?oh=d1d49aea28d3cb6ff76033db1ae056ba&oe=57D998E2" class = "images" id = "three" style = "left: 200px;"></img>
<p class = "para" id = "pthree">3</p>
<img src = "https://scontent-sjc2-1.xx.fbcdn.net/v/t1.0-1/c22.22.272.272/s160x160/166620_1817593049535_5878435_n.jpg?oh=78d8a42398b126fc1f75d1b32295029a&oe=57D73E8C" class = "images" id = "four" style = "left: 300px;"></img>
<p class = "para" id = "pfour">4</p>
<img src = "http://homeinspectioncarync.com/wp-content/uploads/2016/05/trevor-e1464897672300.jpg" class = "images" id = "five" style = "left: 400px;"></img>
<p class = "para" id = "pfive">5</p>
<img src = "http://homeinspectioncarync.com/wp-content/uploads/2016/05/betterrob-e1464897851528.jpg" class = "images" id = "six" style = "left: 500px;"></img>
<p class = "para" id = "psix">6</p>
<img src = "https://upload.wikimedia.org/wikipedia/en/thumb/d/d0/Chrome_Logo.svg/1024px-Chrome_Logo.svg.png" class = "images" id = "seven" style = "left: 600px;"></img>
<p class = "para" id = "pseven">7</p>
<img src = "https://scontent-sjc2-1.xx.fbcdn.net/v/t1.0-9/12373154_1629529113976977_749911928470317591_n.jpg?oh=70dc625bdbd6c6e35406f47cc02bd82e&oe=57D42D84" class = "images" id = "eight" style = "left: 700px;"></img>
<p class = "para" id = "peight">8</p>
<img src = "https://upload.wikimedia.org/wikipedia/en/thumb/d/d0/Chrome_Logo.svg/1024px-Chrome_Logo.svg.png" class = "images" id = "nine" style = "left: 800px;"></img>
<p class = "para" id = "pnine">9</p>
<img src = "https://upload.wikimedia.org/wikipedia/en/thumb/d/d0/Chrome_Logo.svg/1024px-Chrome_Logo.svg.png" class = "images" id = "ten" style = "left: 900px;"></img>
<p class = "para" id = "pten">10</p>
<img src = "https://upload.wikimedia.org/wikipedia/en/thumb/d/d0/Chrome_Logo.svg/1024px-Chrome_Logo.svg.png" class = "images" id = "eleven" style = "left: 1000px;"></img>
<p class = "para" id = "peleven">11</p>
<img src = "https://upload.wikimedia.org/wikipedia/en/thumb/d/d0/Chrome_Logo.svg/1024px-Chrome_Logo.svg.png" class = "images" id = "twelve" style = "left: 1100px;"></img>
<p class = "para" id = "ptwelve">12</p>
<img src = "https://upload.wikimedia.org/wikipedia/en/thumb/d/d0/Chrome_Logo.svg/1024px-Chrome_Logo.svg.png" class = "images" id = "thirteen" style = "left: 1200px;"></img>
<p class = "para" id = "pthirteen">13</p>

Upvotes: 0

Views: 59

Answers (2)

ghost_dad
ghost_dad

Reputation: 1306

So the main reason the two animations weren't activating at the same time is that transition only works when the non-hover state has the property defined that will be animated in the hover state. In your case, you were missing an initial declaration of the top property (thus that wasn't animating). I adjusted things a little to use transform instead, as this provides a smoother transition.

I also wrapped each image and paragraph in a container (which will fix the jittery issues you were seeing), because the image will animate independent of the container and won't be handling the hover event. In addition, I cleaned up quite a bit of markup. See my comments below for best practices there.

.image-container {
  position: relative;
  margin: 0;
  display: inline-block;
}
  
.images {
  height: 100px;
  width: 100px;
  -webkit-transform: translateY(0);
  -ms-transform: translateY(0);
  transform: translateY(0);
  transition: all 1000ms ease;
}

.image-container:hover .images {
  opacity: 0.5;
  -webkit-transform: translateY(-10px);
  -ms-transform: translateY(-10px);
  transform: translateY(-10px);
}

.para {
  position: absolute;
  top: 0;
  left: 0;
  display: inline-block;
  font-size: 200%;
  opacity: 0;
  transition: all 1000ms ease;
}

.image-container:hover .para {
  opacity: 1;
}
<div class="image-container">
  <img src="https://media.licdn.com/mpr/mpr/shrinknp_200_200/p/3/000/00c/2ab/35ae340.jpg" class="images">
  <p class="para">1</p>
</div>
<div class="image-container">
  <img src="http://homeinspectionraleighnc.com/wp-content/uploads/2010/11/Glenns-Photo-1.png" class="images">
  <p class="para">2</p>
</div>
<div class="image-container">
  <img src="https://scontent-sjc2-1.xx.fbcdn.net/v/t1.0-1/p160x160/18718_2753855091076_8128390731521222770_n.jpg?oh=d1d49aea28d3cb6ff76033db1ae056ba&oe=57D998E2" class="images">
  <p class="para">3</p>
</div>
<div class="image-container">
  <img src="https://scontent-sjc2-1.xx.fbcdn.net/v/t1.0-1/c22.22.272.272/s160x160/166620_1817593049535_5878435_n.jpg?oh=78d8a42398b126fc1f75d1b32295029a&oe=57D73E8C" class="images">
  <p class="para">4</p>
</div>
<div class="image-container">
  <img src="http://homeinspectioncarync.com/wp-content/uploads/2016/05/trevor-e1464897672300.jpg" class="images">
  <p class="para">5</p>
</div>
<div class="image-container">
  <img src="http://homeinspectioncarync.com/wp-content/uploads/2016/05/betterrob-e1464897851528.jpg" class="images">
  <p class="para">6</p>
</div>
<div class="image-container">
  <img src="https://upload.wikimedia.org/wikipedia/en/thumb/d/d0/Chrome_Logo.svg/1024px-Chrome_Logo.svg.png" class="images">
  <p class="para">7</p>
</div>
<div class="image-container">
  <img src="https://scontent-sjc2-1.xx.fbcdn.net/v/t1.0-9/12373154_1629529113976977_749911928470317591_n.jpg?oh=70dc625bdbd6c6e35406f47cc02bd82e&oe=57D42D84" class="images">
  <p class="para">8</p>
</div>
<div class="image-container">
  <img src="https://upload.wikimedia.org/wikipedia/en/thumb/d/d0/Chrome_Logo.svg/1024px-Chrome_Logo.svg.png" class="images">
  <p class="para">9</p>
</div>
<div class="image-container">
  <img src="https://upload.wikimedia.org/wikipedia/en/thumb/d/d0/Chrome_Logo.svg/1024px-Chrome_Logo.svg.png" class="images">
  <p class="para">10</p>
</div>
<div class="image-container">
  <img src="https://upload.wikimedia.org/wikipedia/en/thumb/d/d0/Chrome_Logo.svg/1024px-Chrome_Logo.svg.png" class="images">
  <p class="para">11</p>
</div>
<div class="image-container">
  <img src="https://upload.wikimedia.org/wikipedia/en/thumb/d/d0/Chrome_Logo.svg/1024px-Chrome_Logo.svg.png" class="images">
  <p class="para">12</p>
</div>
<div class="image-container">
  <img src="https://upload.wikimedia.org/wikipedia/en/thumb/d/d0/Chrome_Logo.svg/1024px-Chrome_Logo.svg.png" class="images">
  <p class="para">13</p>
</div>

A couple good rules of thumb, after looking through your markup:

  • Only use ID's when they are absolutely necessary (if you're targeting them with JavaScript, and if there really will only be one ever).
  • Use absolute positioning sparingly. With your original code, creating a responsive page would be almost impossible since all the elements were stuck in one place. Instead, keep parent elements in the flow and absolute elements closely coupled.
  • Make sure that html attributes are syntactically correct. You were writing class = "images". Update to class="images".
  • No inline styling. It makes it very difficult to update the page later.

Upvotes: 0

Alan Thomas
Alan Thomas

Reputation: 1034

Those two transitions are happening at the same time. It just seems like the color change is happening after the slide up because the transition time is happening over time as opposed to instantly with the slide.

And if you want to make the transition slower, just change the value in the transition property from 1000ms to something higher. You can use 's' for seconds as well eg. 1s, 2s etc.

Also, instead of creating a separate CSS definition for each ID of each image, use a class that can be applied to all of them, since they all have the same behavior.

Ps. Please use codepen or some similar online code viewer next time you have a question like this. It's much easier to help that way.

Upvotes: 1

Related Questions