Animation with Sass and hover doesn't work? - css

I'm stuck on :hover doesn't work. I suspect the absolute and float position but don't have any clear idea. I'm a beginner user of Sass and the css animation. Any help ?
.menus {
cursor: pointer;
&:hover >.box {
transform: scale(0) translateX(300px);
.check {
transform: rotate(0deg);
}
}
}
.box {
float: right;
position: absolute;
top: 0;
right: 0;
width: 20%;
height: 100%;
background-color: yellow;
border-radius: 4px;
color: green;
transform: scale(1) translateX(0);
transition: transform 330ms ease-in-out;
.check {
display: block;
float: right;
position: absolute;
right: 30%;
top: 35%;
transform: rotate(-360deg);
transition: transform 500ms ease-in-out;
}
}
<div class="restaurants menus">
<div class="box"><img class="check" src="img/check24.png" alt="#"></div>
<h3>Citrus Squid Carpaccio</h3>
<p>with orange zest</p>
<img class="heart " src="img/heart-30.png" alt="#">
</div>

The problem lies here :
.menus {
cursor: pointer;
&:hover + .box {
Your selector &:hover + .box selects the .box elements that are siblings of .menus, and not children. You can use &:hover .box if you want to select all boxes children and grand children of .menus, or &:hover>.box to select direct .box children.
Here's a link with all the css selector that might be useful in the future: https://www.w3schools.com/cssref/css_selectors.asp

Related

css hover an element without affecting :after

How do I stop an pseudo element from being affected by parents hover?
.list-item {
position: absolute;
background: black;
color: white;
padding: 10px 20px;
transition: all 250ms ease-in-out;
}
.list-item:hover {
transform: scale(1.5);
}
.list-item:after {
content: '';
position: absolute;
width: 20px;
height: 20px;
background: orange;
z-index: 10;
border-radius: 100%;
}
<div class="list-item">
test
</div>
When hovering it applies all hover effects to :after pseudo element
One method can be to apply an opposing transition to the ::after pseudo element:
.list-item:hover {
transform: scale(1.5);
}
.list-item:hover::after {
/* transform: scale(calc(1 - (your main scale amount - 1) / 2)); */
transform: scale(0.75);
}

Scss :last-child selector problem with attribute selector

Here is the html:
<div class="card">
<!-- First child -->
<div class="card__side">
Front
</div>
<!-- Last child -->
<div class="card__side--back-1">
Back
</div>
</div>
and here is the scss code:
.card {
perspective: 150rem;
position: relative;
height: 50rem;
&:hover &__side {
&:first-child {
transform: rotateY(180deg);
}
&:last-child {
transform: rotateY(0deg);
}
}
[class*="card__side"] {
font-size: 2rem;
height: 50rem;
transition: all .8s ease;
position: absolute;
width: 100%;
backface-visibility: hidden;
&:first-child {
background-color: $color-white;
}
&:last-child {
background-color: $color-white;
transform: rotateY(180deg);
}
}
}
when I use the class name card__side--back-1, the selector [class*="card__side"] is working but the selector :last-child is not working,
however when I change the class name to card__side for both of the divs, it works.
I know I can give different classes for the task but asking to learn the main reason behind this. This is my first post so I hope I am making it right. Thanks in advance.
when I change the class name to card__side for both of the divs, it works.
I've solved it, the selectors work fine now.
the problem was at the &:hover &__sideselector at line 5.
I've changed the 5th line of the scss code to &:hover [class*="card__side"] now it selects.
.card {
perspective: 150rem;
position: relative;
height: 50rem;
&:hover [class*="card__side"] {
&:first-child {
transform: rotateY(180deg);
}
&:last-child {
transform: rotateY(0deg);
}
}
[class*="card__side"] {
font-size: 2rem;
height: 50rem;
transition: all .8s ease;
position: absolute;
width: 100%;
backface-visibility: hidden;
&:first-child {
background-color: $color-white;
}
&:last-child {
background-color: $color-white;
transform: rotateY(180deg);
}
}
}

CSS TranslateX doesn't move to the right of div

I'm trying to get the toggle to move it 100% to the right. As I'm trying to make it responsive, I can't set it to move an xx amount of pixels.
Can you please help?
input:checked + .slider:before {
-webkit-transform: translateX(100%);
-ms-transform: translateX(100%);
transform: translateX(100%);
}
https://jsfiddle.net/Lc1tdhgb/1/
Thanks
setTimeout(function() {
document.getElementById('togBtn').checked = true;
}, 1000)
#toggle {
width: 100%;
height: 100%;
position: relative;
}
.switch {
position: absolute;
display: inline-block;
width: 100%;
/*min-height: 32px;*/
height: auto;
top: 0;
}
.switch input {
display: none;
}
.slider {
cursor: pointer;
background-color: #ca2222;
-webkit-transition: .5s;
transition: .5s;
border-radius: 32px;
padding: 12px 0;
}
.slider:before {
position: absolute;
content: "";
height: 1.1em;
width: 1.1em;
left: 3px;
bottom: 3px;
background-color: white;
-webkit-transition: .5s;
transition: .5s;
border-radius: 50%;
}
input:checked+.slider {
background-color: #3eab37;
}
input:focus+.slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked+.slider:before {
left: calc(100% - 20px);
/*-webkit-transform: translateX(100%);
-ms-transform: translateX(100%);
transform: translateX(100%);*/
}
/*------ ADDED CSS ---------*/
.slider:after {
content: 'OFF';
color: white;
display: block;
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
font-size: 0.7em;
font-family: Roboto, sans-serif;
}
input:checked+.slider:after {
content: 'ON';
}
/*--------- END --------*/
<div id="toggle">
<label class="switch"><input type="checkbox" id="togBtn"><div class="slider round"></div></label>
</div>
Well, just make sure that the container of the elements follow a position: relative;, so the wrapper have the restrains for the absolute elements inside of it. Then, right is actually how far from right you want the element to be, in this case, you could've used either right: 0%; or left: 100%; although you've encountered the error in the fact that you'd be ignoring margins from the parent's style. That's why I added left: calc(100% - 20px); (20px was on trial and error, until I got it aligned with the outter border of the switch!), then now it works as wanted. Glad to help :)

Weird CSS image hover

I'm creating a hover effect on an image with SCSS on a WordPress site (see gif: https://gyazo.com/1a35247e40d74b5fc756d508de4231eb)
As you see the image gets a "distorted" after hovering over it, maybe the ease-in property is wrong, or I'm not doing the hover effect properly. I'm wondering what I'm doing wrong in the code when it behaves like this.
This is the code that is working:
(left out some SCSS because it was so wast but the & is used to use parent class)
Edit: The HTML & SCSS
<div class="project_container">
<div class="project_content">
Test event
<br>
2018
</div>
<img src="http://testsite.com/wp-content/uploads/2018/12/img.jpg" class="attachment-full aligncenter">
</div>
-
&_container {
position: relative;
height: 300px;
width: 300px;
&:hover {
& > img {
opacity: .2;
}
& > .project_content {
opacity: 1;
}
}
& img {
position: absolute;
width: 100%;
height: 100%;
/*
object-fit: none;
background-size: cover;
background-position: 50% 50%;
background-repeat: no-repeat;
*/
// Hover tranisiton
transition: opacity .5s;
}
}
&_content {
opacity: 0;
transition: opacity .5s;
// Center Position
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: $purple;
z-index: 2;
text-transform: uppercase;
font-size: 20px;
font-weight: 500;
text-align: center;
}
}
So using :hover; on the &_container gives the project_content: opacity: 1;. Then it also blurs the background image with the opacity: .2;, and the effect is achieved with a transition;
Thank you!
I rewrote everything with the hover effect so now I use an overlay class which has opacity: 0; on it (and a transition that takes care of the effect on :hover)
This is a simple version of the HTML:
<div class="project_container">
<div class="project_content">
<p>Content</p>
</div>
<img src="#" alt="test">
<div class="project_overlay"></div>
</div>
and the simplified SCSS:
.project {
&_container {
position: relative;
height: 300px;
width: 300px;
&:hover .project_overlay,
&:hover .project_content {
opacity: 1;
transition: opacity .5s;
}
}
& img {
position: absolute;
width: 100%;
height: 100%;
}
&_overlay {
background-color: rgba(255, 255, 255, .5);
position: absolute;
height: 100%;
width: 100%;
opacity: 0;
transition: opacity .5s;
}
}

animating pseudo element when passing to another element

I got a pseudo-element that marks the user's current choice in a navigation bar. It's a small upward triangle, an icon font from Font-Awesome. here's a jsFiddle DEMO of it (you need to stretch the result panel so everything will be lined).
.subnav > ul > li.active > a:after {
position: relative;
text-align: center;
font-family: FontAwesome;
top: 25px;
right: 50%;
content: "\f0de";
color: #c1c1c1;
}
I've added some basic jQuery function that switches the .active class, and I'm wondering if there's a way to animate the transition of the pseudo element so it'll move horizontally to the new position.
I know pseudo-elements transition are a thing, but searching and googling around I couldn't find anything similar to what I'm looking for. Is this even possible?
In this solution I used the :target pseudo class to switch states, but I recommend you stick with the jQuery function that switches the .active class.
FIDDLE
Markup
<div class="page" id="one">page one</div>
<div class="page" id="two">page two</div>
<div class="page" id="three">page three</div>
<div class="top">
<div class="arrow"></div>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</div>
CSS
.top
{
background: #eee;
position:relative;
overflow: hidden;
}
.arrow
{
border-bottom: 1px solid #c2c2c2;
height: 50px;
}
.arrow:before
{
content: '';
display: block;
width: 14px;
height: 14px;
border: 1px solid #c2c2c2;
border-radius: 3px;
position:absolute;
bottom:-9px;
left: 30px;
background: #fff;
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-ms-transform: rotate(45deg);
-o-transform: rotate(45deg);
transform: rotate(45deg);
-webkit-transition: left, 0.5s;
-moz-transition: left, 0.5s;
-o-transition: left, 0.5s;
transition: left, 0.5s;
}
ul
{
position: absolute;
top: 0;
list-style: none;
padding-left: 20px;
margin-top: 15px;
}
li
{
display: inline-block;
text-decoration: none;
font-size: 18px;
color: #676767;
margin-right: 40px;
}
.page
{
width: 100%;
height: 200px;
position: absolute;
top: 80px;
opacity: 0;
background: yellow;
-webkit-transition: opacity, 0.5s;
-moz-transition: opacity, 0.5s;
-o-transition: opacity, 0.5s;
transition: opacity, 0.5s;
}
.page:target
{
opacity: 1;
}
#two
{
background: pink;
}
#three
{
background: brown;
}
#one:target ~ .top .arrow:before
{
left: 30px;
}
#two:target ~ .top .arrow:before
{
left: 105px;
}
#three:target ~ .top .arrow:before
{
left: 189px;
}

Resources