Jump to content

[SOLVED] OnScroll


jaymc

Recommended Posts

Ok, I have a div around 50 pix in height

 

It has a scroll bar to show the other 49 pictures inside the div, as only 1 is visible without scrolling (each image is 50px)

 

Problem is, only 5% of the time people will scroll the view the other 49 images, thus a waste of time loading them 95% of time as not visible without scroll

 

So, I was going to create a dynamic javascript function to make it so when they scroll, it preloads the other 49 images

 

I can do that no problem, its the OnSCroll even thats the problem

 

If they touch the SCroll bar, I will load the images via the javascript, hence, scrolling executes my function

 

However, if they scroll, then scroll again, the function is executed again. I know the images will probably be cached so no harm done, I just dont want the function executed numerous times after the first initial execution

 

Not to mention that scrolling via the scroll button accounts for an ONSCROLL even everytime it moves a milimetre..

 

They use it on youtube if you have noticed.. When you click the scroll bar to view hidden content the images are preloaded

 

So, in conclusion, how to make blah blah blah happens when the scroll bar/button is touched for the first time, then never again

 

Cheers!

Link to comment
Share on other sites

your code doesn't even make sense; you have not even declared the variable. unless you set a cookie you will have no way to cancel the function, because the function that is associated with onscroll event will keep being triggered. how do you come up with your percentage facts; like 5% of people are scrolling a div - are you using a hit counter for a scroll event or something? :D

Link to comment
Share on other sites

The 5% was a guess, its probably 1%

 

This is declaring the variable right?

 

var touched = 'yes'

 

This is checking the variable?

 

if(touched != 'yes'

 

Im not great with javascript but seems pretty logical.. Are you sure this can not be done without using a cookie..

Link to comment
Share on other sites

var touch is being declared as 'yes' in the else condition, but you do not have it declared above the if/else condition. the if condition does not even know what "touch" is. what are you setting the touch var to "yes" with? if your guessing, that means you really don't know how many people actually are viewing all the pictures.

Link to comment
Share on other sites

you'd want to declare your "touched" variable outside of that function... so it'd be a global variable...

 

var touched=false;
OnScrollDown="if(touched == false){do stuff} else { touched = true;}"

 

then... when next that function is called... it "SHOULD" pick up on the global variable and not run again :D

Link to comment
Share on other sites

you can also try this:

 

<script language="javascript">
var i = 0;
function doEventOnce()
{
var myDIV = document.getElementById('pics');
myDIV.onscroll = function() {
i++
if (i == "1") {
document.getElementById('pics').scrollTop='0';
alert("Remove This Alert And Add Your Preloader Here"); // remove this alert and add your preloaded here
}
else {
// do not preload image again
}
}
}
</script>


<div id="pics" style="border:solid 1px black;width:500px;height:150px;overflow:auto" onscroll="doEventOnce()">
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</div>

Link to comment
Share on other sites

you'd want to declare your "touched" variable outside of that function... so it'd be a global variable...

 

var touched=false;
OnScrollDown="if(touched == false){do stuff} else { touched = true;}"

 

then... when next that function is called... it "SHOULD" pick up on the global variable and not run again :D

Thats the one mate, declaring it globally sorted the issue

 

Cheers!

Link to comment
Share on other sites

you'd want to declare your "touched" variable outside of that function... so it'd be a global variable...

 

var touched=false;
OnScrollDown="if(touched == false){do stuff} else { touched = true;}"

 

then... when next that function is called... it "SHOULD" pick up on the global variable and not run again :D

Thats the one mate, declaring it globally sorted the issue

 

Cheers!

 

that really doesn't make sense to me; post your final code, so I can see what you did.

Link to comment
Share on other sites

<html>
<head>
<script>
var go = "no"
</script>

</head>
<body>


<div id="lol" style="height:100px;overflow:scroll;" OnScroll="if(go == 'no'){alert('hi');go='yes'}else {}">
hey
<BR>
hey
<BR>
hey
<BR>
hey
</div>

 

There you go mate

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.