简体   繁体   中英

Jquery Slide Animation on hover

I am trying to implement a Jquery Hover function on my Company Logo. I want to achieve this :

使用Jquery悬停效果

However, I had achieved THIS

I used the following logic :

$(".m1").hover(function() {
        dir = !dir;
        r = dir? -50 : 0;
        t = dir? 50 : 0;

        $(".slide").stop().animate({left: r+'px'}, 300);

    });

You can check my JS Fiddle here : http://jsfiddle.net/Jiteen/xZ6Hv/ Any sort of Help or Suggestion is Appreciated.

How about the below as a starting point? No need for JS!

Demo Fiddle

HTML

<div>
    <div href="#" class="word" data-text="edia">M</div>
    <div href="#" class="word" data-text="antra">M</div>
</div>

CSS

.word {
    display:inline-block;
    font-size:1em;
    line-height:1;
    position:relative;
    font-size:50px;
}
.word:first-child {
    color:orange;
}
.word:last-child {
    color:grey;
}
.word:after {
    content:attr(text);
    position:relative;
    display:inline-block;
    overflow:hidden;
    max-width:0;
    color:black;
    font-size:20px;
    transition:max-width .5s ease-in;
}
div:hover .word:after {
    max-width:40px;
}

You can achieve this by using a structure like this:

<div class="logo">
    <div class="m1">M</div>
    <div class="m3">aaa</div>
    <div class="m2">M</div>
    <div class="m4">aaa</div>
</div>

And animate it by changing the width of .m3 and .m4

jsFiddle

This is what i would do: http://jsfiddle.net/A6vYy/ .

Do note though, that if you're using images instead of divs you can skip some of the CSS.

JS

$(document).ready(function () {
    $(".logo").on("mouseenter", function () {
        $(this).find(".hidden").animate({width: "50px"}); 
    }).on("mouseleave", function () {
        $(this).find(".hidden").animate({width: "0px"});
    });
});

HTML

<div class="logo">
    <div class="part">M</div><div class="hidden">edia</div><div class="part">M</div><div class="hidden">antra</div>
</div>

CSS

.part, .hidden {
    width: 50px;
    height: 50px;
    background: red;
    display: inline-block;
    font-size: 24px;
    text-align: center;
    vertical-align: top;
}

.hidden {
    width: 0px;
    overflow: hidden;
}

Try this out :

   <div class="media">
        <span class="big">M</span>edia
    </div>
    <div class="mantra">
        <span class="big">M</span>antra
    </div>

Css:

.media,.mantra{

    width:28px;
    overflow: hidden;
    float:left;
    margin-left:2px;
   -webkit-transition: 0.3s linear;
   -moz-transition: 0.3s linear;
   -ms-transition: 0.3s linear;
   -o-transition: 0.3s linear;
   transition: 0.3s linear;
    cursor: pointer;

}

.media:hover{
    display: inline-block;
    height:60px;
    width:60px;
    float:left;
   -webkit-transition: 0.3s linear;
   -moz-transition: 0.3s linear;
   -ms-transition: 0.3s linear;
   -o-transition: 0.3s linear;
   transition: 0.3s linear;
}

.mantra:hover{
    display: inline-block;
    height:60px;
    width:60px;
    float:left;
}


.big{
font-size: 2em;
 color: #ff8800;

}

Working Demo : http://jsfiddle.net/Jiteen/xZ6Hv/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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