What does this bit of javascrtipt do?

I really like how the numbered links work here

The nav is made by this

				<nav id="codrops-demos" class="codrops-demos">
				<a href="#set-1">1</a>
				<a href="#set-2">2</a>
				<a href="#set-3">3</a>
				<a href="#set-4">4</a>
				<a href="#set-5">5</a>
				<a href="#set-6">6</a>
				<a href="#set-7">7</a>
				<a href="#set-8">8</a>
				<a href="#set-9">9</a>
			</nav>

But if I inspect the element, I see…

<nav id="codrops-demos" class="codrops-demos">
				<a href="#set-1" class="current-demo">1</a>
				<a href="#set-2">2</a>
				<a href="#set-3">3</a>
				<a href="#set-4">4</a>
				<a href="#set-5">5</a>
				<a href="#set-6">6</a>
				<a href="#set-7">7</a>
				<a href="#set-8">8</a>
				<a href="#set-9">9</a>
			</nav>

How did the class “current-demo” get there?
in looking at the source, I think this effect is caused by

var hash = window.location.hash,
current = 0,
demos = Array.prototype.slice.call( document.querySelectorAll( ‘#codrops-demos > a’ ) );

		if( hash === '' ) hash = '#set-1';
		setDemo( demos[ parseInt( hash.match(/#set-(\d+)/)[1] ) - 1 ] );

		demos.forEach( function( el, i ) {
			el.addEventListener( 'click', function() { setDemo( this ); } );
		} );

		function setDemo( el ) {
			var idx = demos.indexOf( el );
			if( current !== idx ) {
				var currentDemo = demos[ current ];
				currentDemo.className = currentDemo.className.replace(new RegExp("(^|\\s+)" + 'current-demo' + "(\\s+|$)"), ' ');
			}
			current = idx;
			el.className = 'current-demo'; 
		}
	</script>

But thats just a guess. I really like this effect and seems pretty efficient to me…

It’s caused by this script.

<script>
			var hash = window.location.hash,
				current = 0,
				demos = Array.prototype.slice.call( document.querySelectorAll( '#codrops-demos > a' ) );
			
			if( hash === '' ) hash = '#set-1';
			setDemo( demos[ parseInt( hash.match(/#set-(\d+)/)[1] ) - 1 ] );

			demos.forEach( function( el, i ) {
				el.addEventListener( 'click', function() { setDemo( this ); } );
			} );

			function setDemo( el ) {
				var idx = demos.indexOf( el );
				if( current !== idx ) {
					var currentDemo = demos[ current ];
					currentDemo.className = currentDemo.className.replace(new RegExp("(^|\\s+)" + 'current-demo' + "(\\s+|$)"), ' ');
				}
				current = idx;
				el.className = 'current-demo'; 
			}
		</script>

oh, I’m trying to duplicate it

What issues are you having?

Well, im just trying to understand it so I can do that at
http://ronisfineart.com/new/painting.php
(then ill use a php ssi)

Did you click the “back to codrops article” link in the upper right?

It explains how this works little better and has a link to download the source, rather than trying to rip it out of the actual rendered source.

This is a CSS effect, not JavaScript.

The JavaScript you’re asking about looks like it is part of the scrolling effect to showcase the different styles of the hover effect… not the actual hover effect itself.

thats the effect im trying to use, it works on this page,
http://ronisfineart.com/new/painting.php
but when I go to the next one, the class of the a still is on #1 (shouldn’t it move?)
http://ronisfineart.com/new/painting2.php

I changed the script to

<script>
var hash = window.location.hash,
current = 0,
demos = Array.prototype.slice.call( document.querySelectorAll( '#galleries > a' ) );

if( hash === '' ) hash = '#set-1';
setDemo( demos[ parseInt( hash.match(/painting(\d+)/)[1] ) - 1 ] );

demos.forEach( function( el, i ) {
el.addEventListener( 'click', function() { setDemo( this ); } );
} );

function setDemo( el ) {
var idx = demos.indexOf( el );
if( current !== idx ) {
	var currentDemo = demos[ current ];
	currentDemo.className = currentDemo.className.replace(new RegExp("(^|\\s+)" + 'current' + "(\\s+|$)"), ' ');
}
current = idx;
el.className = 'current'; 
}
  </script>

and the links are made up like this

<div id="galleries">
<a href="painting.php">1</a>
<a href="painting2.php">2</a>
<a href="painting3.php">3</a>
<a href="painting4.php">4</a>
<a href="painting5.php">5</a>
<a href="painting6.php">6</a>
<a href="painting7.php">7</a>
<a href="painting8.php">8</a>
<a href="painting9.php">9</a>
<a href="painting10.php">10</a>
<a href="painting11.php">11</a>
<a href="painting12.php">12</a>
<a href="painting13.php">13</a>
<a href="painting14.php">14</a>
<a href="painting15.php">15</a>
</div>

Just use jQuery, it’s already included in your page.

<script>
$('#galleries > a').each(function() { 
    var _href = $(this).attr('href');
    var _url = document.location;
    
    $(this).removeClass('current'); // just go ahead and remove it from all of them

    if(_url.indexOf(_href) > -1) {
        $(this).addClass('current');
    }
});
</script>

bzw. var _href = this.href;

1 Like

Mawburn’s addition is excellent. I wanted to understand more about the JS version from the posts above, but the script has zero comments.

1 Like

Haha yeah… I tend to overuse sometimes. I just get in that mindset.

Thanks.

you could completely omit the jQuery in the function body (without much of a change), were it not for the missing support of Element.classList in IE.

Mawburn example was short and sweet and seems pretty straightforward.
problem is I added the script

<script>
$('#galleries > a').each(function() { 
var _href = this.href;
var _url = document.location;

$(this).removeClass('current'); // just go ahead and remove it from all of them

if(_url.indexOf(_href) > -1) {
    $(this).addClass('current');
}
});
</script>

to

http://ronisfineart.com/new/painting2.php
to see if the class was added and it wasn’t.

If you look at your console, you’ll see this error:

Uncaught TypeError: undefined is not a function

Which points to painting2.php line 133. That’s if(_url.indexOf(_href) > -1) {.

That error means it’s trying to use a method that’s not available to the object, namely .indexOf because it’s the only method invoked. That method is a pretty common piece of the JavaScript’s String and Array prototype, so that means _url which comes from document.location is neither of those. Which is right, because it’s an Object, not a String. You can read more about document.location, which says:

To retrieve just the URL as a string, the read-only document.URL property can also be used.

so…

var _url = document.URL;

Should do it. I used the wrong one.

Anyway, it was my fault and that’s my thought process on debugging it. Hope it helps.

thanks, that worked. Also I figured out how to get the console installed on Chrome? Is that a good browser for this?

Chrome is great.

It’s already installed. Right click. Hit inspect element. Now click “console”.

1 Like

ahhhhhhh

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.