5
I’m studying effects with Parallax and I’m trying to display a video in the background instead of an image. So I was able to create a good effect using image as background.
Here is my code jQuery:
$('[data-parallax]').each(function(){
var $this = $(this),
$window = $(window);
$window.scroll(function() {
var y = -($window.scrollTop() / $this.data('speed')),
background = '50% '+ y + 'px';
$this.css('background-position', background);
});
});
And mine CSS:
[data-parallax] {
background-attachment: fixed;
background-color: #fff;
background-image: url('http://lorempixel.com/720/480');
background-position: 50% 0;
background-repeat: no-repeat;
background-size: cover;
min-height: 100%;
position: relative;
}
Example: http://jsfiddle.net/7a2ky/show/
Code: http://jsfiddle.net/7a2ky/
I would like to do the same effect, but using a video instead of an image. This is possible?
It’s possible, but you won’t be able to use css background for it. You’ll have to create a video element, fix it and move it around the screen. I recommend you be at the beginning of your HTML, right next to
body
, so you won’t have to change thez-index
because the other elements will be in front of him.– Gabriel Gartz
@Gabrielgartz, thank you. I even did that, but I couldn’t replicate some background-specific and essential properties for working like
background-position: 50% 0
andbackground-size: cover
. Do you have any idea how this can be done?– Caio Tarifa
If you leave the video the size of the screen or the desired size, you can leave the video area at most that will occupy the screen and use the css
transform
to achieve the effect ofcover
, calculating the original ratio to the width and height of the element, as well as its positioning. But it’s not as easy as doing withbackground
because there are no "shortcuts" listed yet.– Gabriel Gartz
I think I’m almost there, @Gabrielgartz. Do you have any idea or example of using the property
transform
to this end?– Caio Tarifa
I used
z-index
for agility purposes in dev, but to improve, next, you can use thetransform
scale()
to achieve its ratio in more is very similar, look at the example.– Gabriel Gartz