I have seen this effect on multiple websites so for reference here are two sites that show the desired result. Locomotive and TUX are two of these, Locomotive might be the best to view as you can instantly see the desired effect hovering over the nav items – although the effect is used throughout the site.
So you have a phrase like "Careers", which on hover scrambles and animations back to the original order of the letters.
I have a version of this I’ve been playing with (attached) but it’s jQuery which I don’t use anywhere else and also it doesn’t seem as ‘scrambled’ as the first letter doesn’t really change – not to mention the width of the word changes.
I’d really appreciate some help creating an effect closer to the examples!
jQuery('document').ready(function($) {
// Set effect velocity in ms
var velocity = 50;
var shuffleElement = $('.shuffle');
$.each(shuffleElement, function(index, item) {
$(item).attr('data-text', $(item).text());
});
var shuffle = function(o) {
for (var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
var shuffleText = function(element, originalText) {
var elementTextArray = [];
var randomText = [];
for (i = 0; i < originalText.length; i++) {
elementTextArray.push(originalText.charAt([i]));
};
var repeatShuffle = function(times, index) {
if (index == times) {
element.text(originalText);
return;
}
setTimeout(function() {
randomText = shuffle(elementTextArray);
for (var i = 0; i < index; i++) {
randomText[i] = originalText[i];
}
randomText = randomText.join('');
element.text(randomText);
index++;
repeatShuffle(times, index);
}, velocity);
}
repeatShuffle(element.text().length, 0);
}
shuffleElement.mouseenter(function() {
shuffleText($(this), $(this).data('text'));
});
});
body {
font-family: helvetica;
font-size: 16px;
padding: 48px;
}
a {
color: black;
text-decoration: none;
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
li {
display: inline-block;
margin: 0 16px 8px 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li>
<a class="shuffle" href="#">shuffle</a>
</li>
<li>
<a class="shuffle" href="#">texts</a>
</li>
<li>
<a class="shuffle" href="#">hover</a>
</li>
</ul>
2
Answers
The first letter already changes(slow down the velocity to see it), to change width it is as easy as adding some letter-spacing. You could always play around with font-size or font-weight.
I added transition all to the anchors just to demonstrate what it looks like but you can be more specific when defining it.
In the js, I added a class and removed it on "animation" end, the letter spacing is added on that class.
If you want to make this more scrambled, you could always call the shuffle 2 times so the length of shuffling seems longer. As a matter of fact, after looking at this a bit more, you are replacing the text when setting it back to the original. It would look better if you could arrange them back to the original text after scrambling it
To avoid layout shifts you also need to disable kerning and ligatures – otherwise the word width will change depending on different letter combinations e.g
fi
(ligature) orAv
(kerning).Here’s a simple vanilla JS function