Is there a way to achieve a parallax effect on an image without using the css background
and background-attachment
properties?
I have an img
tag nested inside a div
and I want to implement a parallax scroll effect on the image. Here is the code snippet:
function resize_div()
{
var image_height = $('.project-image img').height();
var div_height = $('.project-image').height();
var window_height = $(window).height();
var window_width = $(window).width();
$('.project-image').css('height', window_height - 80);
}
$(window).resize(function () {
resize_div();
});
.project-details
{
width:100%;
}
.project-image{
text-align:center
}
.project-description
{
line-height:15px;
margin:0 0 10px
}
.img-responsive
{
height: auto;
max-width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="project-details">
<div class="project-image">
<img src="http://placehold.it/350X225" class="img-responsive">
</div>
<h1>
Project Title
</h1>
<p class="project-description">
Lorem Ipsum is simply dummy text of the printing and typesetting industry... (continued)
issue: The image cannot be placed in the background of a div
, so it must be done using the img
tag only.
Update: The image is centered horizontally within the div
, and both the image and the div
sizes are adjusted when the browser is resized or the device is rotated using custom JavaScript. Attempts with position:absolute
and position:fixed
have not been successful.
https://i.sstatic.net/2WUzE.jpg
Update-2: Check out the fiddle link for more details.