Paula
Paula

Reputation: 35

How to show first slide on a jQuery div slider

I am working on a slide with links on each slide that trigger the sliding movement. but I cant figure out how to make the first slide appear on load page, so to make it work I had to make the first slide visible and the others hidden, it's not a real fix, is just so I can keep working on it, so I need to change it

here is the fiddle

http://jsfiddle.net/Paula/XmeGN/10/

Also, I can make it slide to the left instead of the right.

Thank you all for the help!!

HTML

<div id="banner_container" style="min-height:160px; clear:both;">

    <div id="banner_wrapper">
    <div class="banner_panel2" id="target1">
        <a href="#target1" class="banner_panel">Target 1</a><br/>
        <a href="#target2" class="banner_panel">Target 2</a><br/>
        <a href="#target3" class="banner_panel">Target 3</a><br/>      
        Target 1
    </div>
    <div class="banner_panel" id="target2">
        <a href="#target1" class="banner_panel">Target 1</a><br/>
        <a href="#target2" class="banner_panel">Target 2</a><br/>
        <a href="#target3" class="banner_panel">Target 3</a><br/> 
        Target 2
    </div>
    <div class="banner_panel" id="target3">
        <a href="#target1" class="banner_panel">Target 1</a><br/>
        <a href="#target2" class="banner_panel">Target 2</a><br/>
        <a href="#target3" class="banner_panel">Target 3</a><br/> 
        Target 3
    </div>
</div>
</div>

​ javascript:

jQuery(function($) {

    $('a.banner_panel').click(function() {
        var $target = $($(this).attr('href')),
            $other = $target.siblings('.active');

        if (!$target.hasClass('active')) {
            $other.each(function(index, self) {
                var $this = $(this);
                $this.removeClass('active').animate({
                    left: $this.width()
                }, 500);
            });

            $target.addClass('active').show().css({
                left: -($target.width())
            }).animate({
                left: 0
            }, 500);
        }
    });

});​

css:

* {
    margin:0;
    padding:0;
    list-style:none;
}


#banner_container {
    width:880px;
    position:relative;
    background-color:#E6E6E6;
    margin:30px;
    background-image: linear-gradient(bottom, rgb(207,207,207) 22%, rgb(247,247,247)     82%);
    background-image: -o-linear-gradient(bottom, rgb(207,207,207) 22%, rgb(247,247,247) 82%);
    background-image: -moz-linear-gradient(bottom, rgb(207,207,207) 22%, rgb(247,247,247) 82%);
    background-image: -webkit-linear-gradient(bottom, rgb(207,207,207) 22%, rgb(247,247,247) 82%);
    background-image: -ms-linear-gradient(bottom, rgb(207,207,207) 22%, rgb(247,247,247) 82%);

    background-image: -webkit-gradient(
    linear,
    left bottom,
    left top,
    color-stop(0.22, rgb(207,207,207)),
    color-stop(0.82, rgb(247,247,247))
);
    border:1px #CCC solid;
    border-radius:10px;
    -moz-border-radius:10px;
    padding:20px 10px 10px 10px;
    overflow:hidden;
    }
    

#banner_wrapper{
    width:890px;
    position:relative;
    z-index:5;
    }

div.banner_panel {
    position: absolute;
    height: 100%;
    width: 100%;
    display:none;
}
div.banner_panel2 {
    position: absolute;
    height: 100%;
    width: 100%;
}
​

Upvotes: 2

Views: 2140

Answers (2)

zb&#39;
zb&#39;

Reputation: 8059

Add active to target1, i also will show how to remove the panel2

 <div id="banner_wrapper">
    <div class="active banner_panel" id="target1">
        <a href="#target1" class="active banner_panel">Target 1</a><br/>
        <a href="#target2" class="banner_panel">Target 2</a><br/>
        <a href="#target3" class="banner_panel">Target 3</a><br/>      
        Target 1
    </div>
    <div class="banner_panel" id="target2">
        <a href="#target1" class="banner_panel">Target 1</a><br/>
        <a href="#target2" class="banner_panel">Target 2</a><br/>
        <a href="#target3" class="banner_panel">Target 3</a><br/> 
        Target 2
    </div>
    <div class="banner_panel" id="target3">
        <a href="#target1" class="banner_panel">Target 1</a><br/>
        <a href="#target2" class="banner_panel">Target 2</a><br/>
        <a href="#target3" class="banner_panel">Target 3</a><br/> 
        Target 3
    </div>
</div>

javascript:

jQuery(function($) {

    $('a.banner_panel').click(function() {
        var $target = $($(this).attr('href')),
            $other = $target.siblings('.active');

        if (!$target.hasClass('active')) {
            $other.each(function(index, self) {
                var $this = $(this);
                $this.removeClass('active').animate({
                    left: $this.width()
                }, 500);
            });

            $target.addClass('active').show().css({
                left: -($target.width())
            }).animate({
                left: 0
            }, 500);
        }
    });

});​

$('div.banner_panel.active').show();

fiddle: http://jsfiddle.net/XmeGN/18/

Upvotes: 1

Beetroot-Beetroot
Beetroot-Beetroot

Reputation: 18078

Paula,

  • In the HTML, change class="banner_panel2" to class="banner_panel" and delete the CSS directive for banner_panel2.
  • (optional) Remove CSS directive display:none; from div.banner_panel {...}.
  • Add $("div.banner_panel").eq(0).find('a.banner_panel').eq(0).trigger('click'); after the click handler definition. Adjust one or both of the eq(0) expressions to select any other panel of your choice as the initial panel.

Also, add e.preventDefault(); at the top of the click handler. This inhibits an undesirable effect when a link is clicked that points to the currently active panel.

Javascript should now look like this :

$('a.banner_panel').on('click', function(e) {
    e.preventDefault();
    var $target = $($(this).attr('href')),
        $other = $target.siblings('.active');

    if (!$target.hasClass('active')) {
        $other.removeClass('active').each(function(index, self) {
            var $this = $(this);
            $this.animate({
                left: $this.width()
            }, 500);
        });
        $target.addClass('active').show().css({
            left: -($target.width())
        }).animate({
            left: 0
        }, 500);
    }
});
$("div.banner_panel").eq(0).find('a.banner_panel').eq(0).trigger('click');

DEMO

Users will (probably) see the fly-in effect on page load but maybe that's no bad thing.

Doing it this way ensures that the initial state of the panels is identical to when the user makes a click for him/herself. If, in the future, you change the click handler to do something slightly different then you need to worry less about checking that the HTML/CSS is compatible. Providing everything works fine for user-clicks, then the initial state will also be fine.

Upvotes: 0

Related Questions