How to add image when img tag is less than 100 from top using jquery?









up vote
0
down vote

favorite












I want to add an image when the user scrolling down and mdl-grid is less than 100 from top.



Here is my html



<div class="mdl-grid mdl-shadow--2dp" style="margin-top:4px;">
<div class="mdl-cell mdl-cell--3-col-desktop mdl-cell--3-col-tablet mdl-cell--1-col-phone div-mobile">
<div class="image-div" style="height:200px;width:200px;">
<img class="image-tag" src="" style="height:200px;width:98px;" />
</div>
</div>


Jquery



$(window).scroll(function () 
$('.mdl-grid').each(function ()
var offsetTopPosition = $(this).offset().top - $(window).scrollTop();
if (offsetTopPosition < 100)
$(this).find('img').attr('src', '/rec.jpeg');

);
);
</div>









share|improve this question





















  • So what's wrong with your code?
    – Justinas
    Nov 10 at 20:48










  • @Justinas it's not set the image when I scroll down
    – Liam neesan
    Nov 10 at 20:50










  • I think @Justinas was looking for more detail - do you see any errors in the console; if you put logging statements in the function, can you confirm your offsetTopPosition is being calculated as you expect, that you ever get into your if() condition, etc?
    – Stevangelista
    Nov 11 at 0:23










  • It work jsfiddle.net/mroj1ud5
    – Mohammad
    Nov 11 at 8:20










  • @Mohammad Sorry for delay. I saw that fiddle it is working there. But in mine it is not working
    – Liam neesan
    Nov 12 at 17:22














up vote
0
down vote

favorite












I want to add an image when the user scrolling down and mdl-grid is less than 100 from top.



Here is my html



<div class="mdl-grid mdl-shadow--2dp" style="margin-top:4px;">
<div class="mdl-cell mdl-cell--3-col-desktop mdl-cell--3-col-tablet mdl-cell--1-col-phone div-mobile">
<div class="image-div" style="height:200px;width:200px;">
<img class="image-tag" src="" style="height:200px;width:98px;" />
</div>
</div>


Jquery



$(window).scroll(function () 
$('.mdl-grid').each(function ()
var offsetTopPosition = $(this).offset().top - $(window).scrollTop();
if (offsetTopPosition < 100)
$(this).find('img').attr('src', '/rec.jpeg');

);
);
</div>









share|improve this question





















  • So what's wrong with your code?
    – Justinas
    Nov 10 at 20:48










  • @Justinas it's not set the image when I scroll down
    – Liam neesan
    Nov 10 at 20:50










  • I think @Justinas was looking for more detail - do you see any errors in the console; if you put logging statements in the function, can you confirm your offsetTopPosition is being calculated as you expect, that you ever get into your if() condition, etc?
    – Stevangelista
    Nov 11 at 0:23










  • It work jsfiddle.net/mroj1ud5
    – Mohammad
    Nov 11 at 8:20










  • @Mohammad Sorry for delay. I saw that fiddle it is working there. But in mine it is not working
    – Liam neesan
    Nov 12 at 17:22












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to add an image when the user scrolling down and mdl-grid is less than 100 from top.



Here is my html



<div class="mdl-grid mdl-shadow--2dp" style="margin-top:4px;">
<div class="mdl-cell mdl-cell--3-col-desktop mdl-cell--3-col-tablet mdl-cell--1-col-phone div-mobile">
<div class="image-div" style="height:200px;width:200px;">
<img class="image-tag" src="" style="height:200px;width:98px;" />
</div>
</div>


Jquery



$(window).scroll(function () 
$('.mdl-grid').each(function ()
var offsetTopPosition = $(this).offset().top - $(window).scrollTop();
if (offsetTopPosition < 100)
$(this).find('img').attr('src', '/rec.jpeg');

);
);
</div>









share|improve this question













I want to add an image when the user scrolling down and mdl-grid is less than 100 from top.



Here is my html



<div class="mdl-grid mdl-shadow--2dp" style="margin-top:4px;">
<div class="mdl-cell mdl-cell--3-col-desktop mdl-cell--3-col-tablet mdl-cell--1-col-phone div-mobile">
<div class="image-div" style="height:200px;width:200px;">
<img class="image-tag" src="" style="height:200px;width:98px;" />
</div>
</div>


Jquery



$(window).scroll(function () 
$('.mdl-grid').each(function ()
var offsetTopPosition = $(this).offset().top - $(window).scrollTop();
if (offsetTopPosition < 100)
$(this).find('img').attr('src', '/rec.jpeg');

);
);
</div>






jquery






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 20:41









Liam neesan

6531525




6531525











  • So what's wrong with your code?
    – Justinas
    Nov 10 at 20:48










  • @Justinas it's not set the image when I scroll down
    – Liam neesan
    Nov 10 at 20:50










  • I think @Justinas was looking for more detail - do you see any errors in the console; if you put logging statements in the function, can you confirm your offsetTopPosition is being calculated as you expect, that you ever get into your if() condition, etc?
    – Stevangelista
    Nov 11 at 0:23










  • It work jsfiddle.net/mroj1ud5
    – Mohammad
    Nov 11 at 8:20










  • @Mohammad Sorry for delay. I saw that fiddle it is working there. But in mine it is not working
    – Liam neesan
    Nov 12 at 17:22
















  • So what's wrong with your code?
    – Justinas
    Nov 10 at 20:48










  • @Justinas it's not set the image when I scroll down
    – Liam neesan
    Nov 10 at 20:50










  • I think @Justinas was looking for more detail - do you see any errors in the console; if you put logging statements in the function, can you confirm your offsetTopPosition is being calculated as you expect, that you ever get into your if() condition, etc?
    – Stevangelista
    Nov 11 at 0:23










  • It work jsfiddle.net/mroj1ud5
    – Mohammad
    Nov 11 at 8:20










  • @Mohammad Sorry for delay. I saw that fiddle it is working there. But in mine it is not working
    – Liam neesan
    Nov 12 at 17:22















So what's wrong with your code?
– Justinas
Nov 10 at 20:48




So what's wrong with your code?
– Justinas
Nov 10 at 20:48












@Justinas it's not set the image when I scroll down
– Liam neesan
Nov 10 at 20:50




@Justinas it's not set the image when I scroll down
– Liam neesan
Nov 10 at 20:50












I think @Justinas was looking for more detail - do you see any errors in the console; if you put logging statements in the function, can you confirm your offsetTopPosition is being calculated as you expect, that you ever get into your if() condition, etc?
– Stevangelista
Nov 11 at 0:23




I think @Justinas was looking for more detail - do you see any errors in the console; if you put logging statements in the function, can you confirm your offsetTopPosition is being calculated as you expect, that you ever get into your if() condition, etc?
– Stevangelista
Nov 11 at 0:23












It work jsfiddle.net/mroj1ud5
– Mohammad
Nov 11 at 8:20




It work jsfiddle.net/mroj1ud5
– Mohammad
Nov 11 at 8:20












@Mohammad Sorry for delay. I saw that fiddle it is working there. But in mine it is not working
– Liam neesan
Nov 12 at 17:22




@Mohammad Sorry for delay. I saw that fiddle it is working there. But in mine it is not working
– Liam neesan
Nov 12 at 17:22

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243211%2fhow-to-add-image-when-img-tag-is-less-than-100-from-top-using-jquery%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243211%2fhow-to-add-image-when-img-tag-is-less-than-100-from-top-using-jquery%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto