I have two divs and I want to move both of them at the same time: one to left and other to up.
I did that using the transfom CSS property and I set the translateX and translateY in pixels.
How can I do the same effect when I don't know the height of first div? It's addaptive according to its content.
My real project uses Angular and I want to avoid using pure JQuery (a pure CSS solution will be great!).
EDIT:
I use the class above to animate my second div:
.to-up {
transition-delay: .2s;
transform: translateY(-127px);
}
I came to that value of 127px through this calculation:
div1 height + div1 margin-bottom + div1 borders.
In my real case, the div1 height is addaptive to its content so I don't know how to animate div2 to the top of its parent. How can I do that?
Here's my HTML:
<div class="container">
<div id="item1" class="item">Test1</div>
<div id="item2" class="item">Test2</div>
</div>
My CSS:
.container {
width: 100%;
}
#item1 {
height: 120px;
}
#item2 {
height: 80px;
}
.item {
width: 100px;
border: 1px solid black;
margin: 5px;
transition: all .3s ease-in-out;
-webkit-transition: all .3s ease-in-out;
-moz-transition: all .3s ease-in-out;
-ms-transition: all .3s ease-in-out;
-o-transition: all .3s ease-in-out;
}
.to-right {
transform: translateX(-107%);
}
.to-up {
transition-delay: .2s;
transform: translateY(-127px);
}
And JQuery:
$('.item').on('click', function() {
$('#item1').toggleClass('to-right');
$('#item2').toggleClass('to-up');
});
Finally there's the code working with pixels on Jsfiddle.
Thanks a lot!
This is the better solution that you can do, with Angular in template (don't forget the "goOn" variable initialization in the component):
<div class="container">
<div id="item1" class="item" [class.to-right]="goOn" (click)="goOn = !goOn">Test1</div>
<div id="item2" class="item" [class.to-up]="goOn" (click)="goOn = !goOn">Test2</div>
</div>
Here a working example: https://stackblitz.com/edit/angular-animation-on-click?file=src/app/app.component.html
I think you can't do it without angular (or js) because we're talking about "click" event.
I'm not understanding about what do you want to do without knowing the box heights. Where do you want to move them? I think that without knowing its solution you can't do it.
Problem fixed.
Instead using translateY property to move the second div I used animation with animation-timing-function as linear and animation-fill-mode as forwards.
What did I changed?
Container element now has relative position.
An animation called top was created (set top to 0 on 100%).
The class .move-up changes element position to absolute and call the top animation.
I set .move-up class to the second div on click.
Here's a working poc on Jsfiddle.
HTML:
<div class="container">
<div id="item1" class="item">Test1</div>
<div id="item2" class="item">Test2</div>
</div>
JQuery:
$('.item').on('click', function() {
$('#item1').toggleClass('to-right');
$('#item2').toggleClass('to-top');
});
CSS:
.container {
width: 100%;
position: relative;
}
#item1 {
height: 120px;
}
#item2 {
height: 80px;
margin-top: 0px;
}
.item {
width: 100px;
border: 1px solid black;
margin: 5px;
transition: all .3s ease-in-out;
-webkit-transition: all .3s ease-in-out;
-moz-transition: all .3s ease-in-out;
-ms-transition: all .3s ease-in-out;
-o-transition: all .3s ease-in-out;
}
.to-right {
transform: translateX(-107%);
}
.to-top {
position: absolute;
animation-name: top;
animation-duration: .3s;
animation-delay: .2s;
animation-timing-function: linear;
animation-fill-mode: forwards;
}
#keyframes top {
0% {
top: 100%;
}
100% {
top: 0;
}
}
Related
I've coded it in css where if you hover over a div it expands and show more details about the div, the issue is that whenever I remove the mouse some of the color is still left as lines, I'll attach a picture.
This is the css code where movie card is the details and movie is the div to hover on
.movie-card{
transition: 500ms ease-in-out;
background-color: #a851ff;
opacity: 0;
visibility: hidden;
border: 1px solid #a851ff;
}
.movie-box-content:hover .movie-card{
transition-delay: 250ms;
transition-duration: 500ms;
visibility: visible;
opacity: 1;
}
This is the result of hovering over:
And this is after removing the mouse over it:
its worth noting that after I scroll once, all the lines get removed.
Removing the transition: 500ms ease-in-out; from the movie-card class gets rid of the spurious 'shadows'.
.movie-box {
position: relative;
display: block;
width: 300px;
height: 168.75px;
}
.movie-box-content {
transform: scale(1);
transition: 500ms ease-in-out;
background-image: url('https://wallpaperaccess.com/full/1508305.jpg');
background-repeat: no-repeat;
background-size: 300px 168.75px;
width: 100%;
height: 100%;
rborder-radius: 2%;
}
.movie-box-content:hover {
transition-delay: 250ms;
transition-duration: 500ms;
transform: scale(1.3);
border: 1px solid #a851ff;
box-shadow: #a851ff;
}
.movie-card {
/* transition: 500ms ease-in-out;*/
background-color: #a851ff;
opacity: 0;
visibility: hidden;
}
.movie-box-content:hover .movie-card {
transition-delay: 250ms;
transition-duration: 500ms;
visibility: visible;
opacity: 1;
}
<div class="movie-box">
<div class="movie-box-content">
<div style="width: 100%; height: 100%;"></div>
<div class="movie-card hidden">
<div class="text-center">
<strong>Drama, psycho, crime</strong>
</div>
</div>
</div>
</div>
There is the question of whether this alters anything else visually.
Incidentally, changing scale(1.2) to scale(i) where i is an integer also seemed to remove the problem which perhaps indicates difficulty with mapping CSS pixels (which can take up several display pixels each) so that as the div scales down it 'leaves behind' parts of the CSS pixel. It would be good if someone could explain this phenomenon.
I don't think it's necessary to have both visibility and opacity change as they essentially achieve the same thing. As you have a general transition set on the class, perhaps the two are interfering with each other.
I am currently trying to make content appear when a label is selected. The issue which I am having is that the content which will appear will vary in size based on the data which is being pulled in so I cannot set a specific height which will work in all cases. I thought the best way to work around this was to use min-height but the height doesn't seem to be betting itself based on the length of the size of the content. How can I fix this o that the size will be based on how much content there is.
#block {
background: yellow;
height: 0;
overflow: hidden;
-webkit-transition: height 300ms linear;
-moz-transition: height 300ms linear;
-o-transition: height 300ms linear;
transition: height 300ms linear;
}
label {
cursor: pointer;
}
#showContent {
display: none;
}
#showContent:checked+#block {
min-height: 5px;
}
#showContent:not(:checked) {
height: 0px;
}
<label for="showContent">Show content</label>
<input type="checkbox" id="showContent" />
<div id="block">
Show content ..... Bla bla bla
</div>
jsfiddle
https://jsfiddle.net/sacora9/81m14v1s/14/
Just set the height to auto. Otherwise, you set min-height:5px, but the height is still set to 0, so the calculated height will be 5px. So the solution is to release the height and make it free :
#block {
background: yellow;
height: 0;
overflow: hidden;
-webkit-transition: height 300ms linear;
-moz-transition: height 300ms linear;
-o-transition: height 300ms linear;
transition: height 300ms linear;
}
label {
cursor: pointer;
}
#showContent {
display: none;
}
#showContent:checked+#block {
height : auto; /* <----- Here, instead of min-height: 5px */
}
#showContent:not(:checked) {
height: 0px;
}
<label for="showContent">Show content</label>
<input type="checkbox" id="showContent" />
<div id="block">
Show content ..... Bla bla bla
</div>
EDIT
I've just noticed the animation part. Transition from 0 to auto is tricky, but here's the trick :
#block {
background: yellow;
overflow: hidden;
max-height: 0;
transition: max-height 0.3s cubic-bezier(0,1,0,1);
}
label {
cursor: pointer;
}
#showContent {
display: none;
}
#showContent:checked+#block {
max-height: 1000px;
transition: max-height 0.3s cubic-bezier(1,0,1,0);
}
#showContent:not(:checked) {
height: 0px;
}
<label for="showContent">Show content</label>
<input type="checkbox" id="showContent" />
<div id="block">
Show content ..... Bla bla bla
</div>
On your example, block still has height: 0; applied to it even when showContent is checked. Instead, set the height to auto.
One way to do this while maintaining the animation would be to use max-height instead:
#block {
background: yellow;
max-height: 0px;
overflow: hidden;
-webkit-transition: max-height 300ms linear;
-moz-transition: max-height 300ms linear;
-o-transition: max-height 300ms linear;
transition: max-height 300ms linear;
}
label {
cursor: pointer;
}
#showContent {
display: none;
}
#showContent:checked + #block {
max-height: 100px; /*any value larger than the content */
}
#showContent:not(:checked){
height:0px;
}
.Button_Image {
width: 40px;
-webkit-transition: opacity 0.5s linear;
-moz-transition: opacity 0.5s linear;
-ms-transition: opacity 0.5s linear;
-o-transition: opacity 0.5s linear;
opacity: 1;
}
.Button:hover .Button_Image {
opacity: 0;
}
.Button_Name {
font-size: 18px;
color: black;
line-height: 40px;
-webkit-transition: opacity 0.5s linear;
-moz-transition: opacity 0.5s linear;
-ms-transition: opacity 0.5s linear;
-o-transition: opacity 0.5s linear;
opacity: 0;
}
.Button:hover .Button_Name {
opacity: 1;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<a href="#" class="Button btn btn-success btn-block">
<img class="Button_Image" src="https://i.stack.imgur.com/hiAkR.png">
<span class="Button_Name">Football</span>
</a>
I have a button which has 2 elements inside.
Image of a sport
Name of the sport
When either of these have the css value display: none; the visible one is aligned to center perfectly.
But I needed to add a fade-in-out functionality, so I wasn't able to use display keyword. Instead I went for opacity.
Which resulted these 2 elements to stay side by side even if one is hidden.
How can I center these, when the other one is hidden?
This image has been captured during the transmission event:
The current state is like this:
But I need it like this:
You can achieve what you want with absolute positioning of the image. Is something like this what you want?:
.sportbtn {
border: green 1px solid;
width: 150px;
height: 40px;
position: relative;
line-height: 40px;
}
.sportimg {
/* centered in button */
width: 30px;
transition: left 1s, margin-left 1s;
position: absolute;
left: 50%;
margin-left: -15px; /* half the image width */
margin-top: 5px;
}
.sportname {
transition: opacity 1s;
opacity: 0;
margin-left: 40px;
}
.sportbtn:hover .sportname {
opacity: 1;
}
.sportbtn:hover .sportimg {
margin-left: 0px;
left: 5px;
}
<div class="sportbtn">
<img class="sportimg" src="https://d30y9cdsu7xlg0.cloudfront.net/png/23344-200.png" />
<span class="sportname">Football</span>
</div>
I currently have a rotating image that flips around and has writing on the back using animated css, however what I want is that when the image flips around it changes with another image so it has a solid colour instead of a reversed version of the image.
CSS
.hover-img {
position: relative;
width: 400px;
height: 300px;
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-ms-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
background:url(bbclike/topright.png);
-webkit-transform:rotateY(180deg);
-webkit-transform-style: preserve-3d;
line-height:200px;
text-align:center;
font-size:0;
}
.hover-img:hover{
-webkit-transform:rotateY(0deg);
font-size:14px;
color:white;
background-color:#FF0;
}
HTML
<div class="hover-img">
Text Goes Here
</div>
Just put in hover section whatever you want when user hovers the div... E.g. :
.hover-img:hover{
background:url(---HERE IMAGE 2---);
font-size:14px;
color:white;
background-color:#FF0;
}
Working fiddle demo here
you may want to try this out
http://jsbin.com/tejiduq/1/edit?html,css,output
change the container class, so that you can manipulate DOM class.
//html
<div>
<i class="icon"></i>
</div>
<br>
<input class="button" type="button" value="flip">
//css
div{
}
.icon {
display: inline-block;
width: 30px;
height: 30px;
transition: all 0.5s;
}
div .icon {
background: url("https://www.w3schools.com/images/compatible_chrome.gif") no-repeat;
}
div.ie .icon{
background: url("https://www.w3schools.com/images/compatible_edge.gif") no-repeat;
transform: scaleX(-1)
}
//javascript
$(".button").click(function() {
$("div").toggleClass("ie");
});
Hope this will find you useful.
Probably not the first time you see this question... but I can't solve this problem.
Here is live version
http://jsfiddle.net/LndEh/
If you change height for .projectwrap, you will see what I am trying to achieve. I have tried add clearfix etc.
HTML
<div class="projectwrap">
<img src="http://www.vectortemplates.com/raster/superman-logo-012.png">
<div class="inner"><span>sometext</span></div>
</div>
<div class="projectwrap">
<img src="http://www.vectortemplates.com/raster/superman-logo-012.png">
<div class="inner"><span>some text</span></div>
</div>
<div class="projectwrap">
<img src="http://www.vectortemplates.com/raster/superman-logo-012.png">
<div class="inner"><span>some text</span></div>
</div>
CSS
.projectwrap
{
position: relative;
width: 28%;
height:auto;
float:left;
}
.projectwrap img
{
position: absolute;
width: 100%;
}
.inner
{
width: 100%;
height: 100%;
background-image: url(http://goodlogo.com/images/logos/batman_logo_2574.gif);
background-size: cover;
position:absolute;
z-index: 11;
opacity: 0;
-webkit-transition: opacity 400ms linear;
-o-transition: opacity 400ms linear;
-moz-transition: opacity 400ms linear;
transition: opacity 400ms linear;
}
.inner a
{
float:left;
text-align: center;
display:table;
width: 100%;
height:100%;
}
.inner a span
{
display: table-cell;
vertical-align: middle;
width:100%;
height:100%;
color:#fff;
text-align: center;
text-transform: uppercase;
}
.inner:hover
{
opacity: 1;
-webkit-transition: opacity 400ms linear;
-o-transition: opacity 400ms linear;
-moz-transition: opacity 400ms linear;
transition: opacity 400ms linear;
}
Since the containers are floated and contain absolutely positioned images, they have no height and will float over each other.
If you want all three logos to appear, change the CSS for the images to position:relative
.projectwrap img {
position: relative;
width: 100%;
}
http://jsfiddle.net/LndEh/1/
EDIT:
Another method, if you need to use position:absolute on the images:
Set a minimum height for the .projectwrap divs so that they don't collapse to zero height.Then they will float as expected.
.projectwrap {
position: relative;
width:28%;
float:left;
min-height:5px;height:auto!important;height:5px;
}
http://jsfiddle.net/LndEh/2/
EDIT:
For the additional three (hidden) images, I have changed from using a background image to using the same 100% width method you used for the superman logos. I placed the links over the image by positioning them absolutely.
.inner {
position:relative;
width: 100%;
...
}
.inner a {
position:absolute;
...
}
http://jsfiddle.net/LndEh/3/
EDIT:
I think I see now what you're going for.
I switched from using background-image on .inner to using <img /> and kept your elements positioned absolutely. Does that work better?
http://jsfiddle.net/LndEh/7/