/    Sign up×
Community /Pin to ProfileBookmark

Javascript Fade Issue…

Ok, here’s what I’m trying to do… I’m trying to have a slick fade in / fade out to my buttons on my page, however… it’s getting stuck in certain states.

Say I have two buttons (btn1 and btn2) and attach “fade_in” to onmouseover and “fade_out” to onmouseout… say both my fades takes 300ms…

If I were to fire btn1 fade_in, btn1 fade_out, bt2 fade_in in quick succession (before the 300ml) btn1 gets stuck in the fade in state… and never fades out. It’s not that the events aren’t firing there’s some issue with my function… here’s my code:

(Also, if there’s a better way of doing this, let me know!)


—————————————-

HTML:
—————————————-

[CODE]
<div id=”nav”>
<ul>
<li><a id=”nav-link-home”
onmouseover=”currentOpac(‘nav-link-home’, 100, 300);”
onmouseout=”currentOpac(‘nav-link-home’, 0, 300);”
onfocus=”currentOpac(‘nav-link-home’, 100, 300);”
onblur=”currentOpac(‘nav-link-home’, 0, 300);”
title=”Home” href=”index.php”>
<span></span>Home</a>
</li>
<li><a id=”nav-link-biography”
onmouseover=”currentOpac(‘nav-link-biography’, 100, 300);”
onmouseout=”currentOpac(‘nav-link-biography’, 0, 300);”
onfocus=”currentOpac(‘nav-link-biography’, 100, 300);”
onblur=”currentOpac(‘nav-link-biography’, 0, 300);”
title=”Biography” href=”biography.php”>
<span></span>Bigography</a>
</li>
</ul>
</div>
[/CODE]


—————————————-

javascript:
—————————————-

[CODE]
function currentOpac(id, opacEnd, millisec) {

// standard opacity is 100
var currentOpac = 100;

// if the element has an opacity set, get it
if(document.getElementById(id).style.opacity < 100) {
currentOpac = document.getElementById(id).style.opacity * 100;
}

// call for the function that changes the opacity
opacity(id, currentOpac, opacEnd, millisec)
}

function opacity(id, opacStart, opacEnd, millisec) {
// speed for each frame
var speed = Math.round(millisec / 100);
var timer = 0;

// determine the direction for the blending,
// if start and end are the same nothing happens
if(opacStart > opacEnd) {
for(i = opacStart; i >= opacEnd; i–) {
setTimeout(“changeOpac(” + i + “,'” + id + “‘)”,(timer * speed));
timer++;
}
} else if(opacStart < opacEnd) {
for(i = opacStart; i <= opacEnd; i++)
{
setTimeout(“changeOpac(” + i + “,'” + id + “‘)”,(timer * speed));
timer++;
}
}
}

// change the opacity for different browsers
function changeOpac(opacity, id) {
var object = document.getElementById(id).style;
object.opacity = (opacity / 100);
object.MozOpacity = (opacity / 100);
object.KhtmlOpacity = (opacity / 100);
object.filter = “alpha(opacity=” + opacity + “)”;
}
[/CODE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@DeshibaMay 27.2008 — hmm, i was trying something like this aswell... (you got a bit further with it then i did ?)

but the problem seems to be that the mouseOut doesn't get triggerd because it's still working on the fade_in() function.

if i had some more time i would try to give it something like an indexVariable. just a simple 1/0 switch.

var indexvar= '0'

let the onmouseover make the indexvar +1 and the onmouseout -1. then put ur fade in function under a if/else or while loop. if (indexvar=1) {fade_in()} else {fade_out()}

this should remove the dependency on the onmouse effects. so it checks for the indexnumber to apply a function instead of reaction to the event on its own.

(tbh i don't understand everything you did to the fullest so i don't know where in ur script to do this else i would try it myself ?)
Copy linkTweet thisAlerts:
@DeshibaMay 28.2008 — ok so that failed

took me a while to figure out this script. and a whole while more to see that u actually already did what i suggested except maybe the different functions for the fade in/out... which is totally irrelevant because it doesn't work.

the only thing i can think of is try to make something that detects weather or not your mouse is still on the link and if not triggers a timeout set for 10 milisecs or so to let it fade out.

i don't have the knowledge to implement this tho.
×

Success!

Help @M_Squared spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...