Trigger CSS animations by hovering over a seperate element - html

I'm attempting to create a small animation on an image (the image grows slightly) when a link is hovered over using only CSS3 animations.
The relevant snippets from my code
HTML:
<img id="enterimg" src="img.png" alt="" />
<a id="enterbutton" href="home.php">Enter</a>
CSS:
#enterimg {
width: 350px;
height: 350px;
-webkit-transition: width 1s ease-out, height 1s ease-out;
-moz-transition: width 1s ease-out, height 1s ease-out;
-o-transition: width 1s ease-out, height 1s ease-out;
transition: width 1s ease-out, height 1s ease-out;
}
a:hover ~ #enterimg{width:400px;height:400px;}
I'm sure the transitions themselves are correct, but none of the different "calls" (the last line of CSS) I've tried have worked.
(This is similar to a number of other questions, but I've looked through them and as far as I can tell none of them answer my question)

Thanks to Lokesh Suthar.
The order of the sibling selector required I placed the link first in the markup. Since the selection was written:
a:hover ~ #enterimg{width:400px;height:400px;}
The markup needed to be in that order
<a id="enterbutton" href="home.php">Enter</a>
<img id="enterimg" src="img.png" alt="" />

If you wrap the content in the trigger, then position the content absolutely, you can achieve something similar to triggering a sibbling with CSS. At least it'll look and act that way.
HTML
<a href="">Click Me
<img id="enterimg" src="http://www.druglessdrs.com/wp-content/uploads/2012/07/google-logo-small.jpg" alt="" />
</a>
CSS
a img {
display:block;
position:absolute;
top:80px;
left:0;
border:solid 1px black;
-webkit-transition: width 1s ease-out, height 1s ease-out;
-moz-transition: width 1s ease-out, height 1s ease-out;
-o-transition: width 1s ease-out, height 1s ease-out;
transition: width 1s ease-out, height 1s ease-out;
}
a:hover img {
left:50px;
}
Fiddle

Related

Unexpected animation in CSS [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am working on a static website and have some animations in CSS:
*
{
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
-ms-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}
This is fine and everything and every page is animated in the same way except one of my HTML documents which also contains input elements and a textarea:
<form action="script/contact-form.php" method="post">
<b>Full name:</b><br/>
<input type="text" name="name" /><br/>
<b>Subject:</b><br>
<input type="text" name="subject" /><br/>
<b>E-mail:</b><br>
<input type="text" name="email" />
<br/><br/>
<b>Your comments:</b><br/>
<textarea name="comment"></textarea>
<br/><br/>
<input type="submit" value="Submit" />
</form>
For some reason the entire page is then animated (unlike the other pages, which are using the same stylesheet). I tried disabling this with:
form, input, textarea
{
-moz-transition: none;
-webkit-transition: none;
-o-transition: color 0 ease-in-out;
-ms-transition: color 0 ease-in-out;
transition: none;
}
but it doesn't seem to have any effect. Here is what it looks like: http://notrussian.de/stack/contact-us.html. Clicking on other pages without the form has different effects.
This is starting to drive me nuts and I really hope you guys could help me out.
Thanks in advance.
What is going wrong
You say :
For some reason the entire page is then animated
That's the expected behaviour when you use this code :
* {
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
-ms-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}
The CSS selector * means that any CSS properties for that selector are applied to every element on your web page.
The value all for property transition means that every property of those elements that can be animated will be animated.
So, the combination of CSS selector * and CSS property transition: all means that every property that can be animated will be animated for every HTML element on your web page.
Solution 1 : Animate ONLY what you want to animate
You could do something like this :
/** Get rid of this code
* * {
* -webkit-transition: all 1s ease-in-out;
* -moz-transition: all 1s ease-in-out;
* -o-transition: all 1s ease-in-out;
* -ms-transition: all 1s ease-in-out;
* transition: all 1s ease-in-out;
* }
*/
.animate, .animate * {
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
-ms-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}
Then, in your HTML, add class=animate to every element you want to animate :
<ul class="animate">
...
</ul>
There are many alternative implementations, but this is one of the easiest ones. For the best approach, see "Additional improvements" herebelow.
Solution 2 : Disable animation for what you DON'T want to animate
This should work :
form, input, textarea {
-moz-transition: none;
-webkit-transition: none;
-o-transition: color 0 ease-in-out;
-ms-transition: color 0 ease-in-out;
transition: none;
}
You say you tried it, but I can't find it anywhere in your code when I go to the page you referenced. Anyway, make sure you put it BEHIND the * selector.
Additional improvements
For the sake of performance AND to prevent unexpected behavior, it's best to always set animation ONLY for those properties and those elements you really want to animate.
So, if you want to animate both the width and height of your div elements, you should do something like this :
div {
-moz-transition: width 2s, height 4s;
-webkit-transition: width 2s, height 4s;
-o-transition: width 2s, height 4s;
-ms-transition: width 2s, height 4s;
transition: width 2s, height 4s;
}
If you want to animate just the background of your #navigation li elements, you should do something like this :
#navigation li {
-moz-transition: background 1s ease-in-out;
-webkit-transition: background 1s ease-in-out;
-o-transition: background 1s ease-in-out;
-ms-transition: background 1s ease-in-out;
transition: background 1s ease-in-out;
}

i have issue in css with transition it can't move smoothly in menu bar?

i have issue in CSS it can't move smoothly in menu bar, the menu in this link can anyone help me thanks .
here is the link jsfiddle.
i try this but nothing work :
.div-move{
-webkit-transition: width 1s ease-in-out;
-moz-transition: width 1s ease-in-out;
-o-transition: width 1s ease-in-out;
transition: width 1s ease-in-out;
}
You can't transition to width:auto, but by using max-width is automatically with use the size of your text. Just make sure the :hover width is longer then the text you use so it will all appear. Also I suggest using a transition on both hover and regular classes so you also have a transition when you mouse off the menu. Try changing these classes:
.text-header-move{
max-width: 0px;
float: right;
overflow: hidden;
-webkit-transition: max-width 1s ease-in-out;
-moz-transition: max-width 1s ease-in-out;
-o-transition: max-width 1s ease-in-out;
transition: max-width 1s ease-in-out;
}
.notification-bar:hover .text-header-move{
max-width:400px;
-webkit-transition: max-width 1s ease-in-out;
-moz-transition: max-width 1s ease-in-out;
-o-transition: max-width 1s ease-in-out;
transition: max-width 1s ease-in-out;
}
You want to transform to an "auto" value that was one of the problems.
.notification-bar:hover .text-header-move{
width:auto;
}
Take a look at this fiddle:
http://jsfiddle.net/herrfischerhamburg/jok48kpz/2/
I updated the fiddle to work with a "max-width", i think this is the better solution for you than the fixed width.

I can't move the logo on my website

I've been writing this website for a few days now and have gotten into trouble with my logo position. I can't seem to move it not matter how much I try. Can anyone suggest how I can do this? PS. (I'm still learning css.)
HTML:
<div class="logofx">
<img src="images\logo.png">
</div>
CSS:
.logofx img {
top:300px;
left:50px;
-webkit-transition: all 5s ease;
-moz-transition: all 5s ease;
-o-transition: all 5s ease;
-ms-transition: all 5s ease;
transition: all 5s ease;
}
.logofx img:hover {
-webkit-filter: hue-rotate(333deg);
}
You need position:relative, position:absolute or position:fixed (depending on the specific desired effect*).
By default, elements have position:static and statically-positioned elements are unaffected by left/top/right/bottom adjustments.
*Effects:
position:relative moves the element by the amount specified, but other elements behave like it hasn't moved. If you want other elements to move too, consider using margin properties instead.
position:absolute moves the element to be placed relative to the nearest position:relative container (or the whole document, if there is none).
position:fixed fixes the element in the browser window, using the left/top as coordinates.
Set position: fixed
.logofx img {
position: fixed;
top:300px;
left:50px;
-webkit-transition: all 5s ease;
-moz-transition: all 5s ease;
-o-transition: all 5s ease;
-ms-transition: all 5s ease;
transition: all 5s ease;
}
what about margins instead?
.logofx img {
margin:300px 0 0 50px;
display:block;
-webkit-transition: all 5s ease;
-moz-transition: all 5s ease;
-o-transition: all 5s ease;
-ms-transition: all 5s ease;
transition: all 5s ease;
}
HTMl:
<div class="logo"><div class="logofx">
<img src="images\logo.png">
</div>
</div>
CSS:
.logo{position:relative;}
.logofx{position:absolute; top:10px; left:10px;}
Try this code this will help you .Kindly put the top and left at correct pixel or move where you want using top and left property.

CSS Crossfade image

I have this code that works great. When I pass the mouse over the image it crossfades to another image.
What I want is: when I pass over the mouse the image crossfade and rest like this, I mean, make only 1 transition.
1)image 1
2)onmouseover image 2
3)onmouse out image 2
here my code:
<div id="crossfade">
<img class="bottom" src="images/site3/pasarela1.png" />
<img class="top" src="images/site3/pasarela2.png" />
</div>
here my css code:
#crossfade {
position:relative;
height:250px;
width:400px;
}
#crossfade img {
position:absolute;
left:0;
opacity: 1;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
-ms-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
#crossfade img.top:hover {
opacity:0;
}
From what I get is that you want the :hover state to "stick". Though it's not a pure CSS solution, a solid way to handle this is if you are using jquery's .addClass.
$('.yourimage').mouseover(function() { //hover over your image to trigger the event
$(this).addClass('yourAnimationClass'); //add the animation class to the image you hovered over
});

css3 height transition not working

i have a problem using css3 transitions
how can i make the transition smooth it appears instantly
i want the div box to slowly change its height when i hover over it
the html code
<div id="imgs">
<img src="http://chat.ecobytes.net/img/emoticons/smile.png" alt=":)" title=":)" />
<img src="http://chat.ecobytes.net/img/emoticons/sad.png" alt=":(" title=":(" />
<img src="http://chat.ecobytes.net/img/emoticons/wink.png" alt=";)" title=";)" />
<img src="http://chat.ecobytes.net/img/emoticons/razz.png" alt=":P" title=":P" />
<img src="http://chat.ecobytes.net/img/emoticons/grin.png" alt=":D" title=":D" />
<img src="http://chat.ecobytes.net/img/emoticons/plain.png" alt=":|" title=":|" />
<img src="http://chat.ecobytes.net/img/emoticons/surprise.png" alt=":O" title=":O" />
<img src="http://chat.ecobytes.net/img/emoticons/confused.png" alt=":?" title=":?" />
<img src="http://chat.ecobytes.net/img/emoticons/glasses.png" alt="8)" title="8)" />
<img src="http://chat.ecobytes.net/img/emoticons/eek.png" alt="8o" title="8o" />
<img src="http://chat.ecobytes.net/img/emoticons/cool.png" alt="B)" title="B)" />
<img src="http://chat.ecobytes.net/img/emoticons/smile-big.png" alt=":-)" title=":-)" />
</div>
jsfiddle
I believe you need to set a specified height instead of auto. http://jsfiddle.net/BN4Ny/ this does a smooth expansion. Not sure if you wanted that little close open effect though. I just forked your jsfiddle and added a specified height.
This solution does not need javascript or have the problem of needing to have a fixed height for the container before hand.
This is made possible by using max-height property and setting its value to a high value.
#imgs {
border:1px solid #000;
border-radius:3px;
max-height:20px;
width:100%;
overflow:hidden;
transition: 2s ease;
}
#imgs:hover {
max-height:15em;
}
<div id="imgs">
<img src="https://sslb.ulximg.com/image/405x405/artist/1346353449_4159240d68a922ee4ecdfd8e85d179c6.jpg/e96a72d63f272127d0b6d70c76fd3f61/1346353449_eminem.jpg" />
</div>
Instead of using a set height on a container or using JS (which are both awkward solutions)... You can put the images in list items and work your transition on the li.
If all the images are going to a similar height it means your content inside the container can still be dynamic. For example...
/*
CLOSED
*/
div.container li
{ height:0px;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-ms-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;}
/*
OPEN
*/
div.container:hover li
{ height:30px;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-ms-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;}
Here's how you can do it: http://jsfiddle.net/minitech/hTzt4/
To keep a flexible height, JavaScript is a necessity, unfortunately.
well I'm using this method:
use max height to transition height instead of the height directly...
for example:
div {
height: auto;
max-height:0;
}
.toggle-above-div:hover div {
max-height:0;
}