...

View Full Version : Resolved Flickering issue with Floating layer



satish_j
01-30-2009, 06:53 AM
I have created a floating layer using Javascript that remains always on top of page on scrolling.Issue is,at some point while scrolling,the layer flickers continuously.
The html page is as follows:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>JAVASCRIPT TESTING</title>
<script language="javascript" type="text/javascript" src="FloatingMenu.js">
</script>
<link rel="stylesheet" type="text/css" href="formatting.css">
</head>
<body>
<div id="header1">
<b><font color='Red' size='6pts' style='letter-spacing: 2px;'>Floating Layer Testing</font></b>
</div>


<div id="content">
<p>Like many common software systems, JavaScript has a history of security problems. Many of these problems could allow a person with malevolent intent to steal sensitive information from a visitor. The number and type of such holes in security vary among browsers and operating system versions. Most JavaScript security holes have been caught and fixed, but new ones are being discovered all the time. For a list of current security holes check out your browser's and operating system's Web pages. As a Web site author, it is your responsibility to keep up-to-date on the current status of known security holes in the applications you create.

Signing Scripts
In Chapter 11, I explained that JavaScript does not provide the ability to directly access files on the client computer. This can be a very large hurdle to overcome if you're trying to upload a file to a server from the client computer. Fortunately, file uploading is one of many functional enhancements that signed scripts provide. Signed scripts are specially packaged scripts that have been verified and signed to be correct and non-threatening. These scripts have additional rights on the client computer that allow a programmer to do many things that he wouldn't otherwise be able to.

With the introduction of Netscape 4.0, a new security model was put in place that would allow digitally signed scripts to bypass some of the restrictions that had previously been placed on them. A signed script can request expanded privileges from the visitor and, with the visitor's permission, gain access to restricted data. A signed script requests these additional permissions through LiveConnect, which allows your JavaScript code to communicate with the Java Capabilities API. The security model allows JavaScript to access certain classes in Java in order to extend its functionality while still maintaining tight security for the client.

A digital signature is a fingerprint of the original programmer, and it allows the security model of the browser to detect where (or from whom) it originated. A script signer can be a person or an organization. By signing a script, you acknowledge yourself as the author and accept responsibility for the program's actions. A signed script contains a cryptographic checksum, which is just a special value that ensures the signed script has not been changed. When a digital signature is detected, you are assured that the code has not been tampered with since the programmer signed it.

Once you finish writing a script, you can use the Netscape Signing Tool to digitally sign it. Signing a script does the following:

Unambiguously assigns ownership of the script to a person or organization.

Allows an HTML page to use multiple signed scripts.

Places the signed script into a Java Archive (JAR) file.

Places the source of the script in the JAR file.

Once a user confirms the origin of the script and is assured that it has not been tampered with since its signing, he or she can then decide whether to grant the privileges requested by the script based on the validated identity of the certificate owner and validated integrity of the script.

.</p>
</div>


<div id="movable">
<b>Please Login or Register</b>
</div>
</body>
</html>


JAVASCRIPT FILE IS AS FOLLOWS:



var startY=0;
var currY=startY;
var destY=currY;
var timerID=0;
var tmr_on=0;
var temp;
function floatMenu() {
clearInterval(timerID);
tmr_on=0;
if (document.documentElement.scrollTop) {
temp=document.documentElement.scrollTop; }
else {
temp=document.body.scrollTop; }
destY=temp+startY;
startFloat();
}

function startFloat() {
if(currY==destY) {
clearInterval(timerID);
tmr_on=0;
}
else if(currY<destY) {
currY+=2;
var newY=currY+"px";
document.getElementById("movable").style.top=newY;
if(tmr_on==0) {
timerID=setInterval("startFloat()",10);
tmr_on=1;
}
}
else if(currY>destY) {
currY-=2;
var newY=currY+"px";
document.getElementById("movable").style.top=newY;
if(tmr_on==0) {
timerID=setInterval("startFloat()",10);
tmr_on=1;
}
}
}
window.onscroll=floatMenu;
window.onresize=floatMenu;


AND CSS FILE IS AS FOLLOWS:



#header1 {
background-color: #66ccff;
border: 1px solid #66ccff;
margin-bottom: 5px;
}

#content {
padding: 10px;
border: 1px solid #66ccff;
margin-bottom: 5px;
margin-left: 21%;
height: 800px;
}

#content a {
text-decoration: none;
color: blue;
display: block;
}

a {
text-decoration: none;
color: blue;
}

#movable {
position: absolute;
width: 20%;
background: #fdcfcc;
top: 0px;
left: 0px;
}


At some point while scrolling,the div 'movable' flickers continuously,while at some other point,it is perfectly stable.
Problem is coming on both IE and FF.Any help seriously appreciated...

abduraooft
01-30-2009, 08:12 AM
Please follow http://www.codingforums.com/showthread.php?t=82672. You may edit your above post.

satish_j
01-30-2009, 09:05 AM
Please follow http://www.codingforums.com/showthread.php?t=82672. You may edit your above post.

Sorry for the improper format...Edited the post...

freedom_razor
01-30-2009, 11:59 AM
That seems to sort out the problem [changed 2 to 1]:

else if(currY<destY) {
currY+=1;
var newY=currY+"px";
document.getElementById("movable").style.top=newY;
if(tmr_on==0) {
timerID=setInterval("startFloat()",10);
tmr_on=1;
}
}
else if(currY>destY) {
currY-=1;

The above makes it slower though. To remove the flicker without sacrificing speed, discard above changes and instead:


function startFloat() {
if(currY==destY-1) {
clearInterval(timerID);
tmr_on=0;
}

satish_j
01-30-2009, 12:13 PM
That seems to sort out the problem [changed 2 to 1]:

else if(currY<destY) {
currY+=1;
var newY=currY+"px";
document.getElementById("movable").style.top=newY;
if(tmr_on==0) {
timerID=setInterval("startFloat()",10);
tmr_on=1;
}
}
else if(currY>destY) {
currY-=1;

The above makes it slower though. To remove the flicker without sacrificing speed, discard above changes and instead:


function startFloat() {
if(currY==destY-1) {
clearInterval(timerID);
tmr_on=0;
}


Thanks for your reply..
That definitely seemed to solve the flicker issue,but now if I scroll window up to top(obviously after scrolling down),then the div does not come to the exact 0,0 position,but remains few pixels down.
can you explain me the reason for this..

satish_j
01-30-2009, 12:52 PM
Friend,
Pls help me through this..i think I have got the cause of prob.The solution given by you solves the problem with static HTML.
But,in real scenario,Iam using a php file in the body for Header1 block.e.g


<html>
...
<body>
<?php
require("header1.php");
?>
<div id="content">
<p>Blah---
....
...

And iam stuck here,if i use the static html(as is the case given to you),your sol works perfectly fine,but if i use the php file,the movable div does not shift back to 0,0 position on scrolling up..
I think iam very close to solution..Pls help me out with this..
Thanks..

freedom_razor
01-30-2009, 01:00 PM
How about that then:

function startFloat() {
if(currY==destY-1) currY--;
if(currY==destY) {
clearInterval(timerID);
tmr_on=0;
}

satish_j
01-30-2009, 01:15 PM
Nopes,that didn't solve the problem..

freedom_razor
01-30-2009, 01:25 PM
Well, it does work OK for me in FF3 and IE7. Can you post the link to your page?
The only change you have to make to the script is the last bit of code I posted, none of the ones before. I tested it and that 1px difference that was sometimes appearing is corrected now.

satish_j
01-31-2009, 06:38 AM
Now,this is getting very frustated.I added one alert statement in the js file and fired this function on window load as well:


function floatMenu() {
clearInterval(timerID);
tmr_on=0;
if (document.documentElement.scrollTop) {
temp=document.documentElement.scrollTop; }
else if (document.body.scrollTop) {
temp=document.body.scrollTop; }
alert(temp);
destY=temp;
startFloat();
}
function startFloat() {
---
--
}
window.onload=floatMenu;
window.onscroll=floatMenu;
window.onresize=floatMenu;


function startFloat() remains same.
Now,when I open this html file directly from desktop,then iam getting alert on load as '0'.,whereas if i put this same html file,js file in apache directory and then run it in browser as "http://localhost/Welcome.html",I am getting alert on load as 'undefined'.
Iam going nuts on this.pls help me..
Thanks.

satish_j
01-31-2009, 07:52 AM
Thank you very much to all for ur efforts.My problem is resolved and I feel extremely belated.
I did the foll change in js file to get the prob resolved.Just setted the 'temp' variable to 0 after every If condition.


function startFloat() {
if(currY==destY) {
clearInterval(timerID);
tmr_on=0;
//*****Reset temp to 0.This is very Imp
temp=0;
}
else if(currY<destY) {
currY+=1;
var newY=currY+"px";
document.getElementById("movable").style.top=newY;
if(tmr_on==0) {
timerID=setInterval("startFloat()",1);
tmr_on=1;
//*****Reset temp to 0.This is very Imp
temp=0;
}
}
else if(currY>destY) {
currY-=1;
var newY=currY+"px";
document.getElementById("movable").style.top=newY;
if(tmr_on==0) {
timerID=setInterval("startFloat()",1);
tmr_on=1;
//*****Reset temp to 0.This is very Imp
temp=0;
}
}
}

Thanks once again for your time..

illusia.it
12-09-2009, 03:15 PM
Hello guys, after looking around for quite a while i finally found this! :thumbsup: i copied the code and tried it out, it works just fine, only i was playing around with it to try to make the div scroll up and down faster but i couldnt figure out how to do it:

i tried changing:
currY+=1; into currY+=4;
and
currY-=1; into currY-=4;

it actually works, it scrolls up n down faster but it keeps flickering!! i am kinda new with programming at all sorry for this probably silly question and thank you for your help! :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum