I have noticed a fascinating effect on several websites. To give you an idea, here are two websites that showcase this effect: Locomotive and TUX. Locomotive is particularly interesting to look at as the desired effect can be seen immediately when hovering over the navigation items, although the effect is used consistently throughout the site.
An example of the effect is having a word like "Careers" which, upon hover, scrambles its letters and then animates back to the original order.
I have been experimenting with my own version of this effect (see attached) but it relies on jQuery, which I do not use elsewhere in my projects. Moreover, the letters don't seem as scrambled as the first one because the width of the word changes.
I would really appreciate some assistance in creating an effect similar to the examples provided!
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>