1

Trying to open/close a div with the direction arrow changing on open and close but staying attached to the div as it closes and opens.

http://jsfiddle.net/siiimple/GqJj8/8/

Ideally I would like it to slide to the left as it closes (fast), then open to the right. The close "-" would change to "+" when it closed.

Thanks for any help :)

Justin Young
  • 75
  • 1
  • 1
  • 10
  • Your fiddle gives the error `Uncaught TypeError: Property '#' of object # is not a function`. I'd recommend consulting [the docs](http://api.jquery.com/hide/) on how to properly call `.hide()` – MrOBrian Sep 05 '12 at 19:11

4 Answers4

3

Departed from crowjonah's solution, and came up with something that I think is closer to your spec.

Check it out here:

http://jsfiddle.net/VLNDL/

First, restructured the divs a little so that the expand/contract buttons is a peer of the sliding div; so that it stays atatched to it during the transition:

<div id="intro-wrap">
<div class="open-intro">+</div>
<div class="close-intro">-</div>
<div id="contentWrap">
    <h1 class="main-header">Here is a Title</h1>
    <p class="main-desc">You can write whatever you want about yourself here. You can say you're a superhuman alien ant, arrived from the nether regions of Dwarf-Ant-Dom.</p>
</div>

​ Then refactored the CSS to: 1. make it a little simpler, 2: change around block, relative, float, and absolute so that the buttons are "pinned" to the relative div, and that's it:

    #intro-wrap {
    position: relative;
    z-index: 1;
    border-left: 25px solid rgba(0,0,0,.2);
    width: 200px;
}
#contentWrap{
    background: rgba(0,0,0,.8);
    padding: 15px 40px 25px 30px;
}

#intro-wrap h1 {
    font-family: "PT Sans Narrow";
    font-size: 25px;
    color: #fff;
    font-weight: 700;
    margin-bottom: 0px !important;
    padding-bottom: 10px;
}

#intro-wrap p {
    line-height: 19px;
    color: #999;
}
.open-intro,
.close-intro {
    position:absolute;
    left:200px;
    cursor: pointer;
    width: 25px;
    height: 25px;
    z-index: 50;
    padding-left:15px;
}
.open-intro {
    display: none;
    background: yellow 
}
.close-intro {
    background: red; 
}

The only thing I changed in the js is that I disabled the opacity animation, but you could bring it back -- I just wouldn't target #intro-wrap with it, you should instead target contentWrap with it:

    $('.open-intro').click(function() {
    $('#intro-wrap').animate({
    //opacity: 1,
    left: '0',
  }, 500, function() {
    // Animation complete.
  });
    $('.open-intro').hide();
    $('.close-intro').show();
});


$('.close-intro').click(function() {
    $('#intro-wrap').animate({
    //opacity: 0.25,
    left: '-225',
  }, 400, function() {
    // Animation complete.
  });
    $('.open-intro').show();
    $('.close-intro').hide();
});

J

Jonathan
  • 4,916
  • 2
  • 20
  • 37
  • That works perfect. For some reason when I load it into the active site, it doesn't seem to work. Perhaps I'm loading it wrong. – Justin Young Sep 05 '12 at 22:35
  • do you have additional CSS loading? I'd say that's the most likely: you're overriding some of the CSS above with your own... And the DOM structure is exactly the same as above? If you use chrome (and you should), right-click on the contentWrap div and "inspect element". From here, you can see what CSS the div is inheriting / applying – Jonathan Sep 06 '12 at 16:01
0

http://jsfiddle.net/harmeet_jaipur/ETx7P/

You can do this easily by slideUp/slideDown.

or you can also try this :

http://jsfiddle.net/harmeet_jaipur/7sRRG/1/

AspiringCanadian
  • 1,595
  • 6
  • 24
  • 43
  • I guess what I'm trying to do is have the div slide to the left with the icon attached to the div on closing, have the close icon change to an open icon, which when pushed, will slide out to the right. Here's updated fiddle: http://jsfiddle.net/siiimple/GqJj8/8/ – Justin Young Sep 05 '12 at 19:22
  • http://jsfiddle.net/harmeet_jaipur/4cLCF/ Something like this? I am guessing this may not be the most apt way to do it, maybe you/someone can improve this. – AspiringCanadian Sep 05 '12 at 20:05
0

I generally create a sprite sheet containing both up and down arrows and then have classes for each with different background positions. On toggle then just assign the arrow the class if it's open or closed.

Also the updated fiddle doesn't work at all for me.

Josh Bedo
  • 3,410
  • 3
  • 21
  • 33
0

An easy way to slide left and right is to use jQuery UI! Just add <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> right after jQuery itself and use the function as shown in this example (I forked Harmeets): http://jsfiddle.net/qEBnG/1/ (edit: even better: http://jsfiddle.net/qEBnG/2/)

Then just fill in the function call with your selector and parameters:

$('.open-button').click(function() {
    $('#content').show("slide", { direction: "left" }, 500);
    $('.open-button').hide();
    $('.close-button').show();
});

$('.close-button').click(function() {
    $('#content').hide("slide", { direction: "left" }, 100);
    $('.open-button').show();
    $('.close-button').hide();
});​

You can modify the CSS a bit in order to have the + and - buttons replace each other if you so desire.

As pointed out by josh, you don't necessarily want to load the entire jQuery UI library just for one small use, in which case you can use jQuery's animate function. Here's a fiddle, and here's some code (to stick in the existing click function):

$('#content').animate({
    opacity: 1,
    left: '-900', //to close. use 0 to open
}, 500, function() {}); //500 is the speed. lower is quicker
crowjonah
  • 2,858
  • 1
  • 23
  • 27
  • 2
    jQuery Ui does have a lot of built in functionality but it's very unnessacary to include a file for something so small and when you aren't using more than one thing in the library. If he is already using it this would definitely be the right route. – Josh Bedo Sep 05 '12 at 19:27
  • True that, josh. If you wanna spare yourself the whole library, you can always [customize your download](http://jqueryui.com/download). Otherwise, there are [CSSey ways](http://stackoverflow.com/questions/596608/slide-right-to-left) of sliding things left and right as well. – crowjonah Sep 05 '12 at 19:31