Calculate background-position based on 'background-position:center'? - css

If I construct an image from 5 different pieces (for creating an effect), one simple method is to create 5 different classes with the same styling (I'm using react). The effect I would like to achieve is rotating each of the 5 parts of the image on hover. Here is the jsx:
var sectionStyle = {
width: '100%',
height: '100%',
backfaceVisibility: 'hidden',
backgroundImage: 'url(' + background + ')',
backgroundSize: 'cover',
};
return(
<div className="container1">
<div className="thumb-details"></div>
<div className="part p01">
<figure className="f1" style={sectionStyle}/>
<div className="f2"></div>
</div>
<div className="part p02">
<figure className="f1" style={sectionStyle}/>
<div className="f2"></div>
</div>
<div className="part p03">
<figure className="f1" style={sectionStyle}/>
<div className="f2"></div>
</div>
<div className="part p04">
<figure className="f1" style={sectionStyle}/>
<div className="f2"></div>
</div>
<div className="part p05">
<figure className="f1" style={sectionStyle}/>
<div className="f2"></div>
</div>
</div>
Then I can manually calculate the background-position of each figure class so that each figure image will start from where the previous ended:
.thumbnail {
width: 100%;
height: 20vw;
}
.container1 {
width: 20vw;
height: 20vw;
position: absolute;
/* background-color: blue; */
}
.thumb-details {
max-width: 100%;
height: 20vw;
background-color: yellow;
position: absolute;
left:0;
right:0;
transition: opacity .2s ease-out;
}
.part {
width: 20%;
height: 100%;
position: absolute;
transform: rotateY(0deg);
transform-style:preserve-3d;
transition: all .5s ease-out;
}
.f1 {
transform: rotateY(0deg);
}
.p01 {
left: 0;
}
.p01 figure {
background-position: 0px 0px;
}
.p02 {
left: 20%;
}
.p02 figure {
background-position: -4vw 0px;
}
.p03 {
left: 40%;
}
.p03 figure {
background-position: -8vw 0px;
}
.p04 {
left: 60%;
}
.p04 figure {
background-position: -12vw 0px;
}
.p05 {
left: 80%;
}
.p05 figure {
background-position: -16vw 0px;
}
.container1:hover .part {
transform: rotateY(180deg);
}
It's just 20vw divded by 5, and then I set the background-position of each one (starting from the second) to start where the previous one ended.
This is working fine.
Problem is - the image is not centered, I can't set its total width and height, because if I use "background-position: center", as I would like to, then I cannot track each part of the image anymore.
I hope it doesn't sound complicated. What I theoretically need is to calculate different offsets of the image (5 different offsets as I showed in the CSS) but to calculate it from the image after it's positioned to be centered. It's important because I want the image to be responsive.
Is it possible?
Thanks in advance

Related

Set an image inside CSS spinner and want it to resize it.image should be in static

I am working on an angular application and implemented spinner using CSS.
Now the spinner working as expected.
I have added a background image inside the CSS using background-image:url()
Here, the problem is image also rotate with the spinner.
Here is my CSS
#spinner {
-webkit-animation: frames 1s infinite linear;
animation: frames 1s infinite linear;
background: transparent;
border: .3vw solid #FFF;
border-radius: 100%;
border-top-color: #250463;
background-image:url("../../../../assets/images/svg/img.svg") ;
width: 5vw;
height: 5vw;
opacity: .6;
padding: 0;
position: absolute;
z-index: 999;
justify-content: center;
align-items: center;
}
#keyframes frames {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(359deg);
transform: rotate(359deg);
}
}
.spinner-wrapper {
position: fixed;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
top: 0;
left: 0;
background-color: rgba(0, 0, 0, 0.3);
z-index: 998;
}
html code
<div *ngIf="isLoading" class="spinner-wrapper">
<div fxLayoutAlign="center center" id="spinner">
</div>
</div>
Experiment 1
added another style img
img {
max-width: 5vw;
position: absolute;
max-height: 5vw;
top: 0;
height: 5vw;
width: 5vw;
clip-path: circle(45% at 50% 49%);
}
i have updated my html code as well
<div *ngIf="isLoading" class="spinner-wrapper">
<div fxLayoutAlign="center center" id="spinner">
</div>
<img src="">
</div>
Experiment 2
added background-attachment: fixed; inside spinner.Still the image is rotating
**Experiment 3 **
I made below changes.now its working
added new css
z-index: 8;
justify-content: center;
top:230px;
left: 575px;
.img {
position: absolute;
top: 0;
width: 42px;
height: 48px;
}
html changes
<div *ngIf="isLoading" class="spinner-wrapper">
<div fxLayoutAlign="center center" id="spinner">
</div>
<img src="../../../../assets/images/svg/img.svg">
</div>
One draw back is here.if i change the screen size,spinner and image appears different position
how can i solve this
Now the image showing some where else in the UI.
how can i include image inside the spinner.
How can i stop spinning my image?
Can we set image size
This is my first experience on CSS.If anything wrong please correct me.

Is there a way to animate colour of an image from left to right?

I'm trying to animate a loading icon using an image. What I'm trying to achieve is to have the image animate between two colours from left to right infinitely. This is done in Angular 7. I'm new to animations but as far as I understand I can solve this with CSS animations.
What I want is something similar to the example below. Only I want the grayscale to disappear to the right, and instead of a grayscale I want to pick two different colours.
All help is appreciated!
body {
margin: 0;
}
.effect {
position: relative;
}
.effect-dup {
filter: grayscale(100%);
position: absolute;
overflow: hidden;
top: 0;
left: 0;
width: 0;
animation: width 1s ease-in-out;
animation-fill-mode: forwards;
}
#keyframes width {
from {
width: 0;
}
to {
width: 100%;
}
}
<div class="effect">
<img src="https://placeimg.com/640/480/nature" />
<div class='effect-dup'><img src="https://placeimg.com/640/480/nature" /></div>
</div>
<div class="effect">
<img src="https://placeimg.com/640/480/tech" />
<div class='effect-dup'>
<img src="https://placeimg.com/640/480/tech" />
</div>
</div>
You can consider an extra layer and mix-blend-mode. Simply adjust the coloration and the blending to obtain the color you want. You will no more need to duplicate the image.
body {
margin: 0;
}
img {
width: 200px;
display: block;
}
.effect {
position: relative;
display: inline-block;
overflow:hidden;
}
.effect:before {
content: "";
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
animation: width 2s ease-in-out infinite;
mix-blend-mode: color;
background: linear-gradient(blue,red);
}
#keyframes width {
from {
transform:translateX(-100%);
}
to {
transform:translateX(100%);
}
}
<div class="effect">
<img src="https://placeimg.com/640/480/nature" />
</div>
<div class="effect">
<img src="https://placeimg.com/640/480/tech" />
</div>

Transform with transition twitches in Chrome

I have next code: JSFiddle
<div class="wrap">
<div class="item">
<div class="image" style="background-image: url(http://loremflickr.com/800/800)"></div>
</div>
</div>
<style>
.wrap {
width: 500px;
}
.item {
overflow: hidden;
padding-bottom: 100%;
position: relative;
}
.image {
backround-size: cover;
background-position: center center;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
transition: all .4s ease;
}
.item:hover .image {
transform: scale(1.1)
}
</style>
If you will put mouse on photo, then leave, then put, then leave once again not very fast (waiting while transition finished), sometimes image is twitches. Looks like trnasition sometimes not working.
Does anybody knows, what is the problem of it?
You have a syntax in your code error:
backround-size: cover;
----^
this causes a positioning twitches

Fixed element in transform translate container not working

I have a wrapper box that I want to animate with transform translate but if I do this I can't use fixed element inside.
example :
<div class="wrapper">
<div class="box-content">
<div class="fixed-element">
</div>
</div>
</div>
<style type="text/css">
.wrapper {
transform: translateX(50px);
background: pink;
}
.box-content {
height: 1000px;
background: green;
}
.fixed-element{
position: fixed;
top: 0;
left: 0;
width: 50px;
height: 50px;
background: blue;
}
</style>
https://jsfiddle.net/aobv5azy/
I don't want use javascript, and I want use transform translate. Animate with "left" is not good for performances.

Hover a div behind another div

Imagine this kind of strucure :
<div class="background">
<div class="object"></div>
</div>
<div class="foreground"></div>
My foreground totally overlays my background
In my CSS I would like to change object property on hover (ie .object:hover{} )
Is there way to do that in CSS without moving my object inside foreground or using js ?
Update : My css as asked.
.background { background:url('background.svg') no-repeat; }
.foreground { background:url('foreground.svg') no-repeat 50% 50%; }
.object {
position: absolute;
top:10px;
left:10px;
opacity:1;
}
.object:hover
{
opacity:0.5;
}
The answer is kind of. You can use sibling selector (+), but you must revert order of your divs.
Example CSS:
.background {
position: absolute;
width: 600px;
height: 400px;
background-color: red;
}
.foreground {
position: absolute;
width: 300px;
height: 200px;
left: 150px;
top: 100px;
background-color: green;
z-index: 1;
}
.object {
width: 600px;
height: 400px;
}
.foreground:hover + .background .object {
background-color: blue;
}
and HTML:
<div class="foreground"></div>
<div class="background">
<div class="object"></div>
</div>
Working sample: http://jsfiddle.net/pBYwT/1/
Higher z-index for div:hover which you want to display on top.

Resources