How can I make a div horizontally slide in?

30,152

Solution 1

Based on the example you give, here's it sliding in from the right.. is this what you are after? http://jsfiddle.net/jPneT/208/

EDIT 2017

Too much jQuery

You're right, here's a CSS alternative

.left-right {
    overflow:hidden;
    height:200px;
    width:200px;
    position:relative;
    background-color:#333;
}
.slider {
    width:200px;
    height:200px;
    position:absolute;
    top:0;
    right:-200px;
    background-color:#000;
    color:#fff;
    transition:0.4s ease;
}

.left-right:hover .slider {
  right:0;
}
<div class="left-right">
  <div class="slider">Welcome !</div>
</div>

Solution 2

My answer uses no JavaScript. CSS can handle this automatically for you.

Here's a link to a fork of your code as a working example: http://jsfiddle.net/g105b/Adk8r/11/

There is only a little change from your example. Rather than hiding the element and showing it with display property, the element is placed off-screen using right: -480px (where 480 is the cumulative width), and moving it to right: 0 when the mouse hovers.

Using CSS transitions provides the animation, and support is very good now: http://www.caniuse.com/#search=transition

This technique allows all browsers back to IE6 view and use your website, but users with older browsers will not have an enhanced experience. Unless you require the animation - as in, it is a feature for it to animate - I would suggest using CSS transitions to futureproof your website and use web standards.

Users of deprecated browsers deserve a deprecated experience.

Solution 3

Fiddle: http://jsfiddle.net/BramVanroy/Adk8r/10/

As said: please learn to write logical and correct HTML. Your markup is invalid and unlogical. You should perfect your HTML and CSS and then study JavaScript and jQuery rather than trying to get a hang of everything at once. This code is a pain to the eye.

Here's what's wrong:

  • Try to avoid large chunks of inline style and JavaScript.
  • You use a span where one would use a heading-tag (<h1>Welcome</h1>) and style it via CSS.
  • You use line breaks <br /> where one would use paragraphs:

    <p>This div appears on hover but I would like to slide in from the right instead of just appearing.</p>

  • There's no structure in your code. This is not necessary to create a working website, but it's good practice to give child elements an indent of two or four spaces. This way, it's very clear for yourself which element is which child or parent. The same is true for your CSS rules: it's better to put your selector first and then the rules (indented) like so:

    h1 {
        font-weight: bold;
        font-size: 160%;
    }
    
  • You have a closing </a> tag but there's no opening <a>.

Share:
30,152
surfbird0713
Author by

surfbird0713

Updated on July 09, 2021

Comments

  • surfbird0713
    surfbird0713 almost 3 years

    I currently have a div appearing on hover, but it just pops up rather than sliding in:

    #home-heroImage{
      padding: 0px;
      margin: 0px auto;
      width:980px;
      height: 525px;
      position: relative;
      z-index: 1;
      background-color: #fcba2e;
    }
    
    #home-hero-pop{
      background-color: #ffffff;
      opacity:0.8;
      -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=80)";
      filter: alpha(opacity=80);
      font: 16px Helvetica,Arial,sans-serif;
      font-weight: bold;
      color: #6d6e70;
      text-align: left;
      padding: 10px;
      position: absolute;
      right: 0px;
      top: 0px;
      height: 505px;
      width: 460px;
      z-index: 2;
    }
    

    Fiddle.

    After looking through the posts on SO, I found this example, which would work if I could get it to slide in from the right instead of the bottom. I don't know much about JavaScript or jQuery so the modifications I've tried to make to this code are not producing the desired effect:

    $(document).ready(function(){
        $('.up-down').mouseover(function(){
            $('.default').stop().animate({
                height: 0    
            }, 200);                        
        }).mouseout(function(){
            $('.default').stop().animate({
                height: 200 
            }, 200)    
        })
    
    });
    

    Fiddle.

    I've tried reading several JavaScript articles online but they're over my head right now.