I have a div .box with which has absolute position set at the bottom of the parent div. It does not have the top position set because the height differs for different .box divs. On hover over the parent div, I want to change its top position to 0 with the transition effect.
In the following code transition does not work if I do not define the top position by default. I have tried using top: auto and it still does not work. How can I use transition without defining the top position.
Here's my code:
HTML:
<div class="wrap">
<div class="box">
Box
</div>
</div>
CSS:
.wrap{
position: relative;
height: 200px;
width: 200px;
background: green;
}
.box{
background: yellow;
position: absolute;
bottom: 0;
left: 0;
transition: all 0.9s ease 0s;
top: 50%; /*== does not work without it ==*/
}
.wrap:hover .box{
top: 0;
bottom: 0;
}
Fiddle: http://jsfiddle.net/5hs09apn/
A slight different approach than using top: http://jsfiddle.net/5hs09apn/2/
set the height for the box, and on hover set it to 100%; let the bottom be zero, so you don't even need to use top
.box{
background: yellow;
position: absolute;
bottom: 0;
left: 0;
height:20px;
transition: all 1s ease 0s;
}
.wrap:hover .box{
height:100%;
bottom: 0;
}
.wrap {
position: relative;
height: 200px;
width: 200px;
background: green;
}
.box {
background: yellow;
position: absolute;
bottom: 0;
left: 0;
height: 20px;
transition: all 1s ease 0s;
}
.wrap:hover .box {
height: 100%;
bottom: 0;
}
<div class="wrap">
<div class="box">
Box
</div>
</div>
Add Top property in JQuery getting the current value of the Top of the .box using position().
This will be dynamic allocation of Top and will not effect your condition of varying .box height.
While doing this, you will have to add the hover effect in the JQuery part too, since the Top will be defined here and CSS wont know what to do with the hover.
Check the Fiddle here
This is the JQuery function added:
$(document).ready(function(){
var x = $('.box').position();
var myTop = x.top;
$('.box').css("top",myTop+"px");
$('.box').css("transition","all 0.9s ease 0s");
$('.wrap').bind('mouseover',function(){
$('.box').css("top","0px");
$('.box').css("bottom","0px");
});
$('.wrap').bind('mouseout',function(){
$('.box').css("top",myTop+"px");
$('.box').css("bottom","0px");
});
});
Hope this gives you what you need.
You can use:
.wrap:hover .box{
margin-bottom: 100%;
}
And try with different percentages until you get one you like. It's crude but I think it can work.
Related
I have a div that is positioned using top and left properties. And I need to dynamically switch the positioning from top/left to its transform equivalent. The problem is that when I apply top: 0, left: 0 and equivalent transform property, the div jumps to 0,0 for a split second, before positioning correctly. I understand that changing top and left properties triggers layout redraw.
Here is a example of this behaviour: codepen.
When applying Animation1 for the first time the described problem occurs, but it then works as expected after that.
<div class="red gpu" style="left: 120px; top: 100px;"></div>
<div class="border original">120,100</div>
<div class="border moved">270,180</div>
<button onclick="animate1()">Animate1</button>
<button onclick="animate2()">Animate2</button>
function animate1() {
let div = document.querySelector('div');
div.style.cssText = `
left: 0px;
top: 0px;
transform: translate(120px, 100px) translate(150px, 80px);`;
}
function animate2() {
let div = document.querySelector('div');
div.style.left = `0px`;
div.style.top = `0px`;
div.style.transform = `translate(120px, 100px)`;
}
div {
width: 100px;
height: 100px;
}
.red {
background-color: red;
transform-origin: 50% 50%;
transition: transform 2s;
position: absolute;
}
.border {
border: 1px solid blue;
position: fixed;
}
.original {
left: 120px;
top: 100px;
}
.moved {
left: 270px;
top: 180px;
}
Is there a way to prevent this from happening? I tried applying top/left and transform properties in separate requestAnimationFrames and using translate3d without any luck.
You need to set in the css what the starting state is and then change only the transform property.
function animate1() {
let div = document.querySelector('div');
div.style.transform = `translate(120px, 100px) translate(150px, 80px)`;
}
function animate2() {
let div = document.querySelector('div');
div.style.transform = `translate(120px, 100px)`;
}
div {
width: 100px;
height: 100px;
}
.red {
background-color: red;
transform-origin: 50% 50%;
transition: transform 2s;
position: absolute;
left: 0px; top: 0px;
transform: translate(120px, 100px);
}
.border {
border: 1px solid blue;
position: fixed;
}
.original {
left: 120px;
top: 100px;
}
.moved {
left: 270px;
top: 180px;
}
<div class="red gpu"></div>
<div class="border original">120,100</div>
<div class="border moved">270,180</div>
<button onclick="animate1()">Animate1</button>
<button onclick="animate2()">Animate2</button>
You are not animating top and left so it's changing them abruptly. You could try transition: all 2s; so that it applies to anything, or transition: top 2s, left 2s, transform 2s; to specify only the properties you are using.
NOTE: Animating non-transform properties can have a negative impact on browser performance so do that with caution, if you can set the original position with just the transform (with top/left 0) you would be better off and your original code would probably work as expected.
I want to add an transparent layer over my img on a card when I hover over it, I have done that part but I want it to be cut to the img and not cover the footer on the card. If that makes sence?
this is the card with Hover. As u can see on the card, the img just covers like 90% of the card, I want the hover overlay to do the same
Card when not hover IMG
Card when hover IMG
.card {
position:relative;
width: 350px;
height: 335px;
background-size: contain;
background-repeat: no-repeat;
background-color: #fff;
border-radius: 5px;
margin: 30px;
float: left;
}
#card_oslo{
background-image: url(img/oslo.jpg);
}
#card_oslo:hover{
box-shadow: inset 0 0 0 1000px rgba(0,0,0,.7);
transition: .5s;
}
You should use a pseudo-element for this. Use :after or :before and set it as full size also set the parent with position:relative; then change the opacity of the pseudo element on hover.
Working Demo.
.box {
position:relative;
}
.box:after {
content:"";
/* Set the element as full-size */
position:absolute;
left:0;
top:0;
width:100%;
height:100%;
/* Set bg and hide the element + animation */
background-color:#000;
opacity:0;
transition: all 0.5s ease 0s;
}
.box:hover:after {
/* Show the overlay on hover */
opacity:0.5;
}
/* For the demo */
.box {
width:200px;
height:200px;
background-color:red;
}
<div class="box"></div>
You can set the overlay to
position: absolute;
top: 0;
bottom: XXpx;
left: 0;
right: 0;
where XX is the footer height, then it will cover the whole card and leave the bottom x pixels free. You can also use % values instead of px.
If you want the overlay to contain text you need to put it into an extra div that you can then use as overlay.
I made a simplified version here https://jsfiddle.net/0L9fL1pj/
Been looking for a similar solution and since this thread never got a proper answer (neither proposed answer got me where I wanted and I doubt) but I got some important clues here and I thought I'd provide my current solution so anyone who has a similar problem can benefit.
I made a simple demo here: https://jsfiddle.net/Tdesign/2ynuajk0/14/
HTML:
<div id="imgBox2" class="shade">
<img id="img2" src="https://upload.wikimedia.org/wikipedia/commons/9/9a/Gull_portrait_ca_usa.jpg" width="350" height="335" loading="lazy" >
</div>
CSS:
#imgBox2 {
position: relative;
width: 500px;
}
.shade:hover::after {
content: "";
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 500px;
background-color: rgba(0,0,0,0.5);
}
I am currently working on an "overlay pop up", which appears when I click a certain button.
It works quite well, however I struggle with the opacity
My main overlay div appears over the whole site and I gave it an opacity, so that you can see slightly the page in the background.
Over the overlay I put a content div, which shows the actual content (in that case a password changing request).
Anyway, I don't want the content box being transparent, but no matter what I try (z-index:10, opacity:1, position:relative etc.) it doesn't work.
It is still transparent, because I set up the opacity in the overlay div.
Here is the code:
CSS:
.changePasswordOverlay
{
height: 0%;
width: 100%;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color:#fafafa;
opacity: 0.9;
overflow-y: hidden;
transition: 1s;
}
.passwordOverlayContent {
margin-left:40%;
margin-top:15%;
font-family:'source_sans_proregular';
font-size:15px;
position:relative;
}
HTML:
<div class="changePasswordOverlay">
<div class='passwordOverlayContent'>
.
.
.
</div>
</div>
you need to use rgba in background instead of opacity, because opacity has inheritance properties therefore children will get opacity as well
Note that rgba, stands for Red/Green/Blue/Alpha. and that's the alpha value that will work as your "opacity" value. The greater the alpha value the more opaque will be.
.changePasswordOverlay {
height: 100%; /* changed for demo */
width: 100%;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: rgba(0, 0, 0, .5);
overflow-y: hidden;
transition: 1s;
}
.passwordOverlayContent {
margin-left: 40%;
margin-top: 15%;
font-family: 'source_sans_proregular';
font-size: 15px;
position: relative;
color:white /* demo */
}
<div class="changePasswordOverlay">
<div class='passwordOverlayContent'>
text
</div>
</div>
Opacity applied the div and its children so .passwordOverlayContent will also have the same opacity, use background rgba instead of opacity
background-color: rgba(0, 0, 0, .9);
changed class :
.changePasswordOverlay
{
height: 0%;
width: 100%;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: rgba(0, 0, 0, .9);
overflow-y: hidden;
transition: 1s;
}
move the passwordOverlayContent container after changePasswordOverlay (instead of inside), and change your css to position:fixed to make it "opacity independant"
I'm trying to have it so that when I hover on an <img> tag, a div will appear over it. I want it to be a white overlay with text inside of it.
I cannot make the image a background-image, as much as I would like to. My code uses width:percent/max-width:pixels and height:auto/max-height:pixels, so without the img there, nothing would show up. And to my knowledge, there is no solution to that issue.
I attempted to give the image a unique id and apply a id:hover .class to have the div appear, but it didn't respond to any coding I gave it, let alone work right. I then tried putting the id on a div of its own over putting it on the picture with still no yield.
I also tried to make a div with the image as a background pic and made the hover as desired. I tried to make the div not implode by putting in another div that has the image constraints, but because of the height:auto, it didn't work.
I refuse to set height/width as pixels, as it would mess up the rest of my coding and one of the major reasons I'm coding what I am. So, if it's not possible because of this, that's fine; Just tell me.
My CSS is as follows:
#logo {
text-align: center;
width:100%;
max-width:769px;
height:auto;
overflow:hidden;
}
#bannerpic {
max-width:769px;
max-height:300px;
width:100%;
height:auto;
}
#bannerpic .logobody {
display:none;
}
#bannerpic:hover .logobody {
display:inline;
background-color:rgba(255,255,255,0.5);
width:100%;
height:100%;
}
My HTML is this:
<div id="logo">
<img id="bannerpic" src="https://dl.dropboxusercontent.com/u/67673862/logoTEMP.png">
<div class="logobody">text</div>
</img>
</div>
I don't know if you need to be able to click the image, but you can overlay text with absolute positioning.
#logo {
text-align: center;
height: auto;
overflow: hidden;
position: relative;
max-width: 469px;
}
#bannerpic {
width: 100%;
height: auto;
}
.logobody {
position: absolute;
background-color: pink;
top: 0;
left: 0;
right: 0;
bottom: 0;
opacity: 0;
-webkit-transition: opacity .3s;
transition: opacity .3s;
}
.logobody:hover {
opacity: 1;
}
<div id="logo">
<img id="bannerpic" src="https://dl.dropboxusercontent.com/u/67673862/logoTEMP.png" />
<div class="logobody">text</div>
</div>
How about this:
#logo {
position: absolute;
z-index: 1;
left: 0;
top: 0;
}
.logobody {
position: absolute;
z-index: 2;
left: 0;
top: 0;
background-color: rgba(255,255,255,0.75);
visibility: hidden;
height: 100%;
width: 100%;
}
#logo:hover .logobody{
visibility: visible;
}
#bannerpic {
max-width:769px;
max-height:300px;
width:100%;
height:auto;
}
Here's a JSFiddle link for it:
https://jsfiddle.net/zVBDc/790/embedded/result/
Note: My example is using 0.75 alpha for the background. 0.5 seemed too low. Set it to whatever you prefer, though.
Im trying to understand CSS Transition - but I cant make it happen. I want to make a line go from 0 to 100% width with a small delay on pageload. I understand that CSs transition needs a trigger, and tried to read up to a jQuery or Vanilla Js solution, but get stuck.
Why is this ot working? Jsfiddle: https://jsfiddle.net/1fzzgnwy/
<div class="container">
<span class="h-line"></span>
</div>
.h-line {
position: absolute;
z-index: -1;
width: 0%;
height: 10px;
right: 0;
background-color: #000;
top: 400px;
transition: right 1.5s linear;
transition-delay: 2s;
}
.h-line.ready {
width: 100%;
background-color: #000;
}
.container {
height:100%;
width:100%;
}
$(function(){
$('.h-line').addClass('ready');
});
In your transition line, you are telling it to transition right, but that doesn't change in the added class. Your width does. So you need to transition the width like transition: width 1.5s linear;: JS Fiddle
And if you want it to expand left to right, remove right: 0: JS Fiddle
In your jsfiddle link (https://jsfiddle.net/1fzzgnwy/) there are some errors.
Here a simplified example of your code that work https://jsfiddle.net/50t3qwmL/ .
.h-line {
position: absolute;
top: 0;
right: 0;
width: 20px;
height: 20px;
background-color: #000;
transition: .3s;
transition-delay: 2s;
}
.h-line.ready {
width: 100%;
}