I have tried to make rotating rays but when it rotate you can see the rays is not on full screen it has empty area from the right and left
you can see the result and code from here: JSFiddle
<style>
#me {
-webkit-animation: rotation 5s infinite linear;
}
#-webkit-keyframes rotation {
from {-webkit-transform: rotate(0deg);}
to {-webkit-transform: rotate(359deg);}
}
.win-boxx-container{
z-index:1111111111111111111;
position: fixed;
width:100%;
height:100%;
background-size:100%;
left:0;
top:0;
background: rgba(0, 0, 0, 0.75);
}
.win-lights-bg {
width: 100%;
height: 100%;
left: 0%;
top: 0%;
left: -59%;
top: -57%;
position: absolute;
z-index:11111111111111111112;
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 810px -926px;
}
.win-boxx-box{
position:absolute;
margin:auto;
background:url('images/win-boxx.png');
background-size:100%;
background-repeat: no-repeat;
width: 572px;
height: 337px;
text-align:center;
margin: auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
z-index:1111111111111111111123;
}
.centering{
text-align:center;
}
body{
position:fixed;
}
#media all and (max-width: 1000px) and (min-width: 520px) {
.win-boxx-box{
width: 352px;
height: 227px;
}
.win-boxx-stars{
width: 50%;
}
}
#media all and (max-width: 550px) and (min-width: 220px) {
.win-boxx-box{
width: 352px;
height: 227px;
}
.win-boxx-stars{
width: 50%;
}
}
.win-lights-bg{
width: 100%;
height: 100%;
left: 0;
top: 0;
position: absolute;
z-index: 0;
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 810px -926px;
}
</style>
<div class="win-boxx-container" >
<img id="me" src="http://store1.up-00.com/2015-01/1421959145661.png" class="win-lights-bg" />
</div>
You have a lot of overlapping, unnecessary CSS in your code.
What I did here is create a fucntion that determines the size your spinner should be based on the current window size and sets it up in vanilla js (javascript).
JSFIDDLE DEMO
Script:
function setUpSpinner() {
//get height and width and the spinning element and assign them to variables
var height = window.innerHeight;
var width = window.innerWidth;
var winlightsbg = document.getElementsByClassName('win-lights-bg')[0];
determine the largest and the adjustment factor (I used 1.5 to accomodate the corners when rotating - you may be able to finesse this a bit)
var largest;
if (height > width) {
largest = height;
} else {
largest = width;
}
var adjust = largest * 1.5;
//set up new height, width and offsets
winlightsbg.style.height = adjust + "px";
winlightsbg.style.width = adjust + "px";
winlightsbg.style.left = -((adjust - largest) / 2) + "px";
winlightsbg.style.top = -((adjust - largest) / 2) + "px";
}
//on page load run our spinner set up function once then bind it to the window resize event
window.onload = function() {
setUpSpinner();
window.addEventListener('resize', setUpSpinner, false);
};
CSS:
.win-lights-bg {
width: 100%;
height: 100%;
position: absolute;
z-index:100;
}
Related
Im trying to make a sidebar with variable width, but for some reason the #media rules are not triggering when changing manually the size of the bar:
In this simplifyed example, the background-color should change when the sidebar width is less than 350px, according to the following rule, but is not working, can't understand why
#media only screen and (max-width: 350px) {
.sidebar {
background-color: aqua;
}
}
Here it is a working code example:
let sidebar = document.querySelector(".sidebar");
let dragholder = document.querySelector(".dragholder");
function onMouseMove(e) {
sidebar.style.cssText = `width: ${ e.pageX }px`;
}
function onMouseDown(e) {
document.addEventListener('mousemove', onMouseMove);
}
function onMouseUp(e) {
document.removeEventListener('mousemove', onMouseMove);
}
dragholder.addEventListener('mousedown', onMouseDown);
dragholder.addEventListener('mouseup', onMouseUp);
document.addEventListener('mouseup', onMouseUp);
.sidebar {
height: 100vh;
position: fixed;
top: 0;
left: 0;
background-color: #2D2D30;
width: 450px;
color: #FFF;
padding: 5px;
box-sizing: border-box;
}
.dragholder {
position: absolute;
height: 100vh;
width: 5px;
background: yellow;
right: -5px;
top: 50%;
transform: translate(-50%, -50%);
border-radius: 80px;
}
.dragholder:hover {
cursor: col-resize;
}
#media only screen and (max-width: 350px) {
.sidebar {
background-color: aqua;
}
}
<div class="sidebar"> TEST 123
<div class="dragholder"></div>
</div>
I am making a hover effect on the image. I set the image to grayscale by default and when it hovered over, a circle follows the cursor and shows the colored part. Basically, there are two images. grayscale one is shown by default and on hover inside the circle, the colored part is shown.
Everything is working good except when I try to size the image using background-size the circle part doesn't follow. As the background property sets the circle part image according to its size. See the code:
I set the background-size of video card to 100% to fill up its parent container but when I do it for the circle, the image is sized inside the circle.
$('.video-card').mousemove(function(e) {
var offs = $(this).offset(),
p = {
x: offs.left,
y: offs.top
},
mPos = {
x: e.pageX,
y: e.pageY
},
x = mPos.x - p.x - 75,
y = mPos.y - p.y - 75;
$('.gray', this).css({
left: x,
top: y,
backgroundPosition: -x + 'px ' + -y + 'px'
});
});
.video-card {
position: relative;
width: 100%;
height: 950px;
overflow: hidden;
background-size: 100% !important;
}
.video-card-overlay {
content: "";
top: 0;
left: 0;
width: 100%;
height: 100%;
position: absolute;
background: gray;
filter: grayscale(100%);
background-size: 100% !important;
}
.gray {
position: absolute;
top: 0;
left: 0;
width: 150px;
height: 150px;
display: none;
border-radius: 50%;
}
.video-card:hover>.gray {
display: block;
}
.video-gallery-section .container {
max-width: 100%;
width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="video-card" style="background: url('assets/img/home/1.png') no-repeat">
<div class="video-card-overlay" style="background: url('assets/img/home/1.png') no-repeat"></div>
<div class="gray" style="background: url('assets/img/home/1.png') no-repeat"></div>
</div>
How about using clip-path instead of trying to achieve the same effect through positioning?
const $overlay = $('.video-card-overlay');
$('.video-card').mousemove(function (e) {
$overlay.css({
clipPath: `circle(150px at ${e.offsetX}px ${e.offsetY}px)`
})
});
.video-card {
height: 950px;
position: relative;
width: 100%;
}
.gray,
.video-card-overlay {
background-image: url('assets/img/home/1.png');
background-position: center center;
background-size: cover;
inset: 0;
position: absolute;
}
.gray {
filter: grayscale(100%);
}
.video-card:not(:hover) .video-card-overlay {
display: none;
}
<div class="video-card">
<div class="gray"></div>
<div class="video-card-overlay"></div>
</div>
See how much shorter the code became!
Until now I have a simple loading bar:
setInterval(tick, 10);
var width = 0;
function tick() {
var el = document.getElementById('bar_full');
width += 1;
width = width > 97 ? 97 : width;
document.getElementById('bar_full').style.width = width + '%';
}
#bar_shell {
width: 100%;
height: 30px;
background: lightGray;
border-radius: 20px;
}
#bar_full {
width: 0%;
margin-top: 5px;
margin-left: 5px;
height: 20px;
background: green;
position: absolute;
border-radius: 10px;
}
<div id="bar_shell">
<div id="bar_full"></div>
</div>
However, I would like it to change colors as it goes up. For example, when it is at 10%, it will be a dark green, at 50% it will start to turn yellow, and at 100% it will be red. I would like to solve this with css, but js is accepted. Thanks.
You can use Css annimation , using key frames , see below snippet :
set also animation fill mode to forwards to maintain final annimation state .
#bar_shell {
width: 100%;
height: 30px;
background: lightGray;
border-radius: 20px;
}
#bar_full {
width: 0%;
margin-top: 5px;
margin-left: 5px;
height: 20px;
background: green;
position: absolute;
border-radius: 10px;
-webkit-animation: changecolor 5s forwards; /* Safari 4.0 - 8.0 */
animation: changecolor 5s forwards;
animation-timing-function: ease-in-out;
-webkit-animation-timing-function: ease-in-out;
}
#-webkit-keyframes changecolor {
0% {width: 0; background:blue}
25% {width: 25%;background:yellow}
50% {width: 50%;background:orange}
75% {width: 75%;background:red}
100% {width: 100%;color:green}
}
/* Standard syntax */
#keyframes changecolor {
0% {width: 0; background:blue}
25% {width: 25%;background:yellow}
50% {width: 50%;background:orange}
75% {width: 75%;background:red}
100% {width: 97%;background:green}
}
<div id="bar_shell">
<div id="bar_full"></div>
</div>
Just set the background color depending on width. One way would be using an object with percentages and accompanying color:
setInterval(tick, 10);
var width = 0;
var colors = {
'0': '#cfeff0',
'10': '#bbc6ce',
'50': '#660066',
'80': '#f6546a'
};
function tick() {
var el = document.getElementById('bar_full');
width += 1;
width = width > 97 ? 97 : width;
var el = document.getElementById('bar_full');
el.style.width = width + '%';
var color = colors[width];
if (color) el.style.backgroundColor = color;
}
#bar_shell {
width: 100%;
height: 30px;
background: lightGray;
border-radius: 20px;
}
#bar_full {
width: 0%;
margin-top: 5px;
margin-left: 5px;
height: 20px;
background: green;
position: absolute;
border-radius: 10px;
}
<div id="bar_shell">
<div id="bar_full"></div>
</div>
I've an image and I have put a mask over it.
The mask is divided into 3 equal parts. Each part has the same image as its background.
Each part has 1/3rd portion of image. I am changing background position of each part, so the masks (collectively) look exactly like the image.
Everything works great, but 2nd part has some issue in background-image, and it looks few pixels shifted to the right.
I need to remove that shifting.
Demo
HTML:
<div id="wrapper">
<div id="main">
<img src="http://images.all-free-download.com/images/graphiclarge/grasshopperpraying_mantis_195444.jpg" />
</div>
<div id="mask">
<div class="part part1"></div>
<div class="part part2"></div>
<div class="part part3"></div>
</div>
</div>
<br>
<button id="switcher" class="main">Put mask on top</button>
CSS:
img {
width: 100%;
height: 100%;
}
#wrapper {
position: relative;
width: 390px;
height: 300px;
}
#main {
z-index: 2;
}
#mask {
z-index: 1;
}
#main, #mask, .part {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.part {
background-image: url('http://images.all-free-download.com/images/graphiclarge/grasshopperpraying_mantis_195444.jpg');
background-size: 390px 300px;
width: 130px;
}
.part2 {
background-position: -130px 0;
left: 130px;
}
.part3 {
background-position: -260px 0;
left: 260px;
}
JS:
$('#switcher').click(function () {
if ($(this).hasClass('main')) {
$(this).html('Put mask on top').removeClass('main');
$('#main').css({
zIndex: 1
});
$('#mask').css({
zIndex: 2
});
} else {
$(this).html('Put main on top').addClass('main')
$('#main').css({
zIndex: 2
});
$('#mask').css({
zIndex: 1
});
}
});
It is because you are using image of dimension 425x318 and resizing background to 390x300, so image gets distorted as aspect ratios of them do not match.
Check updated demo, here I have used wrapper dimension of same size as of image, i.e. 425x318.
JS Fiddle Demo
Update in code:
CSS
Remove background-size from .part and change #wrapper width, height.
img {
width: 100%;
height: 100%;
}
#wrapper {
position: relative;
width: 425px;
height: 318px;
}
#main {
z-index: 2;
}
#mask {
z-index: 1;
}
#main, #mask, .part {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.part {
background-image: url('http://images.all-free-download.com/images/graphiclarge/grasshopperpraying_mantis_195444.jpg');
/*background-size: 390px 300px;*/
width: 130px;
}
.part1 {
background-position: 0 0;
left: 0;
}
.part2 {
background-position: -130px 0;
left: 130px;
}
.part3 {
background-position: -260px 0;
left: 260px;
}
UPDATE:
As you mentioned in comment that Image is not in control, background-size properties contain and cover also not working.
The only feasible option seems to get the image dimensions by some method (javascript OR server side code) and dynamically set the #wrapper dimensions to match image dimensions.
JavaScript Code: (For getting image dimensions and set Wrapper dimensions accordingly)
http://jsfiddle.net/8au8nhe5/19/
$(document).ready(function() {
var myImage = new Image();
myImage.src = "http://images.all-free-download.com/images/graphiclarge/grasshopperpraying_mantis_195444.jpg";
$(myImage).on('load', function() {
console.log('My width is: ', this.naturalWidth);
console.log('My height is: ', this.naturalHeight);
$("#wrapper").css({"width": this.naturalWidth + "px", "height": this.naturalHeight + "px"});
});
});
For Reference:
http://davidwalsh.name/get-image-dimensions
https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement
As pointed out by Dharmang, cause of the issue was aspect ratio of image. And, it was not possible to fix, without changing the #wrapper dimension, or the image itself.
I've found a workaround, but using img tag, instead of background-image, so that I can distort the aspect ratio of images of mask div, in equal amount, as it is destroyed by the 'real image'.
$('#switcher').click(function () {
if ($(this).hasClass('main')) {
$(this).html('Put mask on top').removeClass('main');
$('#main').css({
zIndex: 1
});
$('#mask').css({
zIndex: 2
});
} else {
$(this).html('Put main on top').addClass('main')
$('#main').css({
zIndex: 2
});
$('#mask').css({
zIndex: 1
});
}
});
img {
width: 100%;
height: 100%;
}
#wrapper {
position: relative;
width: 390px;
height: 300px;
}
#main {
z-index: 2;
}
#mask {
z-index: 1;
}
#main, #mask, .part {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.part {
width: 130px;
}
.part img {
margin-left: 0;
width: 390px;
height: 300px;
}
.part2 {
left: 130px;
}
.part2 img {
margin-left: -130px;
}
.part3 {
left: 260px;
}
.part3 img {
margin-left: -260px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">
<div id="main">
<img src="http://images.all-free-download.com/images/graphiclarge/grasshopperpraying_mantis_195444.jpg" />
</div>
<div id="mask">
<div class="part part1"><img src="http://images.all-free-download.com/images/graphiclarge/grasshopperpraying_mantis_195444.jpg" /></div>
<div class="part part2"><img src="http://images.all-free-download.com/images/graphiclarge/grasshopperpraying_mantis_195444.jpg" /></div>
<div class="part part3"><img src="http://images.all-free-download.com/images/graphiclarge/grasshopperpraying_mantis_195444.jpg" /></div>
</div>
</div>
<br>
<button id="switcher" class="main">Put mask on top</button>
This is my CSS class:
.overitem {
position: relative;
background: url(images/bg.png) no-repeat;
width:83px;
height: 83px;
}
.sit {
position: absolute;
top: 50%;
left: 50%;
}
My HTML :
<div class="overitem"><img src="/images/vcs.png" class="sit"/></div>
The problem is that I can not make the from the img tag, in a middle of the background of overitem class. What should I do, to make it appear in the middle of the bg.png image?
Since the .sit class has to have a fixed width and height (because its an image), you can use the following method:
.sit {
position: absolute;
top: 50%;
left: 50%;
width: 500px;
height: 500px;
margin-top: -250px; /* Half the height */
margin-left: -250px; /* Half the width */
}
Source: http://css-tricks.com/snippets/css/absolute-center-vertical-horizontal-an-image/
you can set margins as
1/2(width of "overitem" minus width of "sit") and
1/2(height of "overitem" height width of "sit")
or try this:
.sit {
position: absolute;
top: 50%;
left: 50%;
margin-left: -(HALF_OF_WIDTH_OF_IMAGE);
margin-top: -(HALF_OF_HEIGHT_OF_IMAGE);
}
In the .sit class you need to have:
margin-left: -(half of width);
margin-top: -(half of height);
An alternative option is:
.overitem {
position: relative;
background: url(images/bg.png) no-repeat;
width:83px;
height: 83px;
line-height: 83px;
}
.sit {
vertical-align: middle;
}
Try this:
elems = document.getElementsByClassName('sit');
for( i = 0, j = elems.length; i < j; i++){
elems[ i ].style.marginLeft = ( elems[ i ].parentNode.offsetWidth - elems[ i ].offsetWidth ) / 2;
elems[ i ].style.marginTop = ( elems[ i ].parentNode.offsetHeight - elems[ i ].offsetHeight ) / 2;
}