I have this function that works well for lazy loading.
panel.find('img[data-src]').each(function(){
element = $(this);
element.attr('src', element.data('src'));
element.removeAttr('data-src');
How can I give a fadeIn()
effect to that removeAttr function?
I tried:
element.removeAttr('data-src').fadeIn();
but it doesn't work. The img
code looks like this and I simply want the dot.png to fadeOut and the original.jpg to fade in.
<img src="dot.png" data-src="original.jpg">
/
Thanks in advance
I have this function that works well for lazy loading.
panel.find('img[data-src]').each(function(){
element = $(this);
element.attr('src', element.data('src'));
element.removeAttr('data-src');
How can I give a fadeIn()
effect to that removeAttr function?
I tried:
element.removeAttr('data-src').fadeIn();
but it doesn't work. The img
code looks like this and I simply want the dot.png to fadeOut and the original.jpg to fade in.
<img src="dot.png" data-src="original.jpg">
http://jsfiddle/7s1yb1un/6/
Thanks in advance
- 2 What do you actually want to achieve ? – Niranjan Kumar Commented Nov 14, 2016 at 15:23
- @NiranjanKumar as I said I want to src to fade out and the data-src to fade in when the img is in view. – Federico Commented Nov 14, 2016 at 15:24
- Post this in jsfiddle – Niranjan Kumar Commented Nov 14, 2016 at 15:26
- @NiranjanKumar here it is: jsfiddle/7s1yb1un/6 – Federico Commented Nov 14, 2016 at 15:34
- jsfiddle/s2194r8L try this – Niranjan Kumar Commented Nov 14, 2016 at 16:01
5 Answers
Reset to default 10You cannot fade a src change on an img
element. To acheive this you'll need two img
elements. The second one will have a the src "original.jpg"
and will have a higher z-index
and start with display: none
for a style. Then you can fade it in and it will fade in over the dot.
EDIT Given your new question, you could do the following:
- Add an
onload
listener for the image - Just before changing the "src", fade the image out
- Then change the "src" to "original.jpg"
- In your
onload
function, do afadeIn
Here is what I have done.
Added a fadeOut(5000)
, the img with original src will fadeout after 5 sec
.
Called a function with timeout of 6sec
, which changes the src
with data-src
and fadeIn(5000)
in 5 sec, I hope this solves your problem.
JS code is below
var myVar;
function myFunction() {
myVar = setTimeout(function(){
var src = $("img.hide").attr("data-src");
$("img.hide").attr("src",src);
$("img.hide").fadeIn(5000);
}, 6000);
}
function myStopFunction() {
clearTimeout(myVar);
}
$(document).ready(function() {
$(".hide").fadeOut(5000);
myFunction();
});
The following code would fade out, change the src, then fade in.
JSFiddle
HTML
<div id="fullpage">
<div class="section">
<img class="fadeable" src="http://1.gravatar./avatar/767fc9c115a1b989744c755db47feb60?size=800" data-src="http://2.gravatar./userimage/5/ff5263e8c30557b57e64423ee8496e41?size=800" width=100 height=100 alt="smile"></div>
</div>
JS
$(function() {
$('img[data-src]').each(function(i, e) {
// cache element
original_img = $(e);
original_img
.fadeOut(function(){
original_img.attr('src', original_img.attr('data-src'))
})
.fadeIn();
})
});
Thanks guys. I found this script working (somehow), the images just blink sometimes. I don't know if semantically correct.
$(function() {
$('img').one("load", function() {
var e = $(this);
e.data('src', e.attr('data-src'));
e.animate({"opacity": 0}, 400);
e.data('src');
e.animate({"opacity": 1}, 400);
})
});
The following code would clone the images that have the data-src
attribute, then hide the clone, update the clone src
, position it over the original image, and fade in. Would this work for you?
JSFiddle
HTML
<div id="fullpage">
<div class="section">
<img class="fadeable" src="http://1.gravatar./avatar/767fc9c115a1b989744c755db47feb60?size=800" data-src="http://2.gravatar./userimage/5/ff5263e8c30557b57e64423ee8496e41?size=800" width=100 height=100 alt="smile"></div>
</div>
JS
$(function() {
$('img[data-src]').each(function(i, e) {
// cache element
original_img = $(e);
// get position of original image
offset_left = original_img.offset().left;
offset_top = original_img.offset().top;
// get data-src of
data_src = original_img.attr('data-src');
// clone original image
original_img.clone()
.hide()
// put it directly in the body, so it can be positioned
.appendTo('body')
// set the new src
.attr('src', data_src)
// place it over the original image
.css({
"left": offset_left,
"top": offset_top,
"position": "absolute"
})
.fadeIn(function(){
original_img.attr('src', data_src);
$(this).remove();
});
})
});