Skip to content

Instantly share code, notes, and snippets.

@meena-erian
Last active January 16, 2017 02:32
Show Gist options
  • Select an option

  • Save meena-erian/8d848ea6807a79bf57d89e86f1912a84 to your computer and use it in GitHub Desktop.

Select an option

Save meena-erian/8d848ea6807a79bf57d89e86f1912a84 to your computer and use it in GitHub Desktop.
Fade in and fade out javaScript functions
// fade.js
// Fade in and fade out javaScript functions
//
// Copyright (C) 2016 Meena Erian
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
function fadeIn(elem) {
elem.style.opacity = 0;
var lid = setInterval(nextFadeIn,50, elem);
function nextFadeIn(elem) {
if(elem.style.opacity>=1) {
elem.style.opacity = 1;
clearInterval(lid);
}
else elem.style.opacity = parseFloat(elem.style.opacity) + 0.1;
}
}
function fadeOut(elem) {
elem.style.opacity = 1;
var lid = setInterval(nextFadeOut,50, elem);
function nextFadeOut(elem) {
if(elem.style.opacity<=0) {
elem.style.opacity = 0;
clearInterval(lid);
}
else elem.style.opacity = parseFloat(elem.style.opacity) - 0.1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment