Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Javascript using fade on changing innerHTML

Featured Replies

Hey guys,

I've been learning some javascript and i've tried to make my own little script for changing my header's text. I don't really know if it is the right way to do it but it works so far.

But now i'm kinda stuck. Although everything works i would like to add a fading effect on the changes for a more subtle and professional look.

I don't really know how to do it though. I hope someone can point me in the right direction?

You can view the result here (just a rough example)

function replaceText()
 	{
		if(teller==1){
lettre.innerHTML="D"; lettre.style.color="#966ea1"; lettre.style.left="500px"; lettre.style.top="200px";

bigtext.innerHTML="Dynamic"; bigtext.style.left="780px"; bigtext.style.top="230px";

smalltext.innerHTML="Development"; smalltext.style.left="900px"; smalltext.style.top="230px"; 
	}
		
		else if(teller==2){
lettre.innerHTML="T"; lettre.style.color="#759da0"; lettre.style.left="100px"; lettre.style.top="200px";

bigtext.innerHTML="Top Ranking"; bigtext.style.left="420px"; bigtext.style.top="250px";

smalltext.innerHTML="On search engines"; smalltext.style.left="610px"; smalltext.style.top="260px";
					   	  }
		
		else if(teller==3){
lettre.innerHTML="G"; lettre.style.color="#976866"; lettre.style.left="370px"; lettre.style.top="100px";

bigtext.innerHTML="General"; bigtext.style.left= "790px"; bigtext.style.top="210px";
	  					   
smalltext.innerHTML="Pc & Mac support"; smalltext.style.left="790px"; smalltext.style.top="240px";
					   	  }
		
		else if(teller==4){
lettre.innerHTML="C"; lettre.style.color="#9cba89"; lettre.style.left="100px"; lettre.style.top="125px";
	  					   
bigtext.innerHTML="Creative"; bigtext.style.left="400px"; bigtext.style.top="180px";
	 					   
smalltext.innerHTML="Design"; smalltext.style.left="680px"; smalltext.style.top="170px"; 							teller=0;}
		
 		teller++;
 	}

 


Thanks in advance

Edited by jochemke

Hello Jochemke,

 

repeating block of code is usually bad practice.

I would suggest to use an array with options and some loops.

This way it will be easier later on to edit your code.

 

To add the fading effect you could use a library like jQuery (checkout .fadeOut() and .fadeIn()).

Just put lettre, bigtext and smalltext inside a container and fade its opacity.

 

Here is some code to get you started...

 

var elements = [
	document.getElementById("lettre"),
	document.getElementById("bigtext"),
	document.getElementById("smalltext")
];

var textOptions = [
	// The first element is used as an index for "elements".
	// For example 0 refers to elements[0], thus "lettre".
	[0, "D", "T", "G", "C"]
];

var styleOptions = [
	[0, "color", "#966EA1", "#759DA0", "#976866", "#9CBA89"],
	[0, "left", "500px", "100px", "370px", "100px"]
	// Add as much properties as you like.
];

var teller = 0;

var interval = setInterval(function(){

	// $("container").fadeOut();

	// Reset "teller" if needed.
	if(teller == (textOptions[0].length - 1) ) teller = 0;

	// Loop through "textOptions".
	for(var i = 0; i < textOptions.length; i++){

		// Get the correct element from "elements".
		var currentElement = elements[textOptions[i][0]]

		// Change its innerHTML.
		currentElement.innerHTML = textOptions[i][teller + 1];
	}

	// Loop through "styleOptions".
	for(var j = 0; j < styleOptions.length; j++){

		var cs = styleOptions[j];
		var currentElement = elements[cs[0]]

		// Change its style.
		currentElement.style[cs[1]] = cs[teller + 2];
	}

	teller++;

	// $("container").fadeIn();
}, 2000);

 

Léon

  • Author

Thanks for the big answer. I will need some time to look at this to fully understand it though i can't believe i didn't see myself that i could better use arrays instead :o. I'm still learning javascript and other oo programming languages.

Thanks for the big answer. I will need some time to look at this to fully understand it though i can't believe i didn't see myself that i could better use arrays instead :o. I'm still learning javascript and other oo programming languages.

Good luck learning, if you have any questions you can send me a pm in dutch.

 

Léon

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.