简体   繁体   English

在鼠标悬停时重新排列文本/字母

[英]Rearrange Text/Letters On Mouseover

I'd like for some text on my page to have the letters rearranged when a user hovers over it. 我希望页面上的某些文本在用户将鼠标悬停在其上方时重新排列字母。 For example, mouseover "WORK," and it becomes "OWKR." 例如,将鼠标悬停在“ WORK”上,它将变为“ OWKR”。 I suspect js is needed, but I'm still pretty new to js. 我怀疑需要js,但是我对js还是很陌生。 Below is my html: 以下是我的html:

<div class="header">
<div id="access" role="navigation">
<ul id="nav">
    <li id="work"><a href="work.html" title="Portfolio">WORK</a></li>
    <li id="studio"><a href="studio.html" title="About Us">STUDIO</a></li>
    <li id="contact"><a href="mailto:someone">CONTACT</a></li>
    <li id="news"><a href="news.html" title="Goings Ons">NEWS</a></li>
</ul>
</div>
</div>

I found a post that helped at Combining .hover() and .replaceWith() and expanded that code for my list. 我找到了一个有助于将.hover()和.replaceWith()组合在一起的帖子,并将该代码扩展到了我的列表中。 However, I have a feeling my js is not as clean as it could be and the other issue I'm having is that on mouseover, the text is no longer a link. 但是,我觉得我的js不够干净,我遇到的另一个问题是鼠标悬停时,文本不再是链接。 Here's my js: 这是我的js:

$(document).ready(function() {   
   $("#work").hover(
      function () {
        $('#work').text('KROW');
      },
      function () {
        $('#work').text('WORK');
      }
    );
    $("#studio").hover(
      function () {
        $('#studio').text('DUTIOS');
      },
      function () {
        $('#studio').text('STUDIO');
      }
    );
    $("#contact").hover(
      function () {
        $('#contact').text('ANOTTCC');
      },
      function () {
        $('#contact').text('CONTACT');
      }
    );
    $("#news").hover(
      function () {
        $('#news').text('ENSW');
      },
      function () {
        $('#news').text('NEWS');
      }
    );
});

I know it's probably not great, but this was the only way I could get it to rearrange like I wanted. 我知道这可能不太好,但这是我可以按自己的意愿重新排列的唯一方法。

Here's the jsfiddle 这是jsfiddle

Thanks for any help! 谢谢你的帮助!

You can simple use the css property content to provide the same effect. 您可以简单地使用css属性content来提供相同的效果。 It is easy to use. 这个用起来很简单。

content:"YOUR TEXT"

Here is my jsfiddle 这是我的jsfiddle

Check it and let me know if there any problem. 检查一下,让我知道是否有任何问题。

Here is one more way, You can associate data to all the anchor links and toggle the text on mouseover and mouseout event. 这是另一种方法,您可以将数据关联到所有锚点链接,并在mouseover和mouseout事件上切换文本。

Here is the live demo . 这是现场演示

You can just add one common class (eg "jumble-up-text") to these anchor links and bind "mouseover", "mouseout" event on it. 您只需向这些锚链接添加一个通用类(例如“ jumble-up-text”),然后在其上绑定“ mouseover”,“ mouseout”事件。

HTML : HTML:

<li><a class="jumble-up-text" href="work.html" title="Portfolio" data-changetxt="KROW">WORK</a></li>

Javascript : Javascript:

$("a.jumble-up-text").on("mouseover mouseout", function(){
    var $this = $(this);
    var sShowTxt = $this.data('changetxt');
    $this.data('changetxt', $this.text());   
    $this.text(sShowTxt);

});

This code will overwrite the anchor tag text on mouseover and set it back to its original value on mouseout event. 这段代码将在鼠标悬停时覆盖定位标记文本,并在mouseout事件时将其设置回原始值。

You can re-use the same code for other anchor links, you just have to attach same class and set the "data-changetxt" attribute value to the the text you want to display on mousehover. 您可以对其他锚链接重新使用相同的代码,只需附加相同的类并将“ data-changetxt”属性值设置为要在鼠标悬停时显示的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM