0

I'm new to jquery so please stay with me and be gentle.

I've used the .cycle code as a navigation and i would like to click a button i.e. "about us" and it take you to the div containing the "about us" contents

I have this working but noticed i've lost my animation, i know what the problem is but because of my lack of knowledge i don't know how to fix it and these forums have been good to me learning this code, so i thought i would ask.

Here is the javascript code i have, like i say, this works and diverts me to the chosen div, what it doesn't do is show the slide effect anymore. I would also like it to show and hide the divs.

<script type="text/javascript" charset="utf-8">
$(document).ready(function () {
    $('#container').cycle({
        fx: 'scrollHorz',
        speed: 'fast',
        timeout: 0,
    });
    jQuery('.index_navigation_1').click(function () {
        window.location.href = '#about_us_container';
        return false;
    });
    jQuery('.index_navigation_2').click(function () {
        window.location.href = '#contact_us_container';
        return false;
    });
});
</script>

Here is the HTML code

<div id ="container">
  <div id="index_container">
    <div id="header">home</div>
    <div id="index_navigation">
      <div id="nav_buttons" class="index_navigation_1">ABOUT US</div>
      <div id="nav_buttons" class="index_navigation_2">CONTACT US</div>
      <div id="nav_buttons" class="index_navigation_3">BUTTON 3</div>
      <div id="nav_buttons" class="index_navigation_4">BUTTON 4</div>
    </div>
    <div id ="about_us_container">
    <div id="header">about us</div>
    </div>
    <div id="navigation">
      <div id="nav_buttons" class="about_us_navigation_1">ABOUT US BUTTON 1</div>
      <div id="nav_buttons" class="about_us_navigation_2">ABOUT US BUTTON 2</div>
      <div id="nav_buttons" class="about_us_navigation_3">ABOUT US BUTTON 3</div>
      <div id="nav_buttons" class="about_us_navigation_4">ABOUT US BUTTON 4</div>
      <div id="nav_buttons" class="about_us_navigation_5">ABOUT US BUTTON 5</div>
    </div>
    <div id="contact_us_container">
      <div id="header">contact us</div>
    </div>
    <div id="navigation">
      <div id="nav_buttons" class="contact_us_navigation_1">CONTACT US BUTTON 1</div>
      <div id="nav_buttons" class="contact_us_navigation_2">CONTACT US BUTTON 2</div>
      <div id="nav_buttons" class="contact_us_navigation_3">CONTACT US BUTTON 3</div>
      <div id="nav_buttons" class="contact_us_navigation_4">CONTACT US BUTTON 4</div>
      <div id="nav_buttons" class="contact_us_navigation_5">CONTACT US BUTTON 5</div>
    </div>
  </div>
</div>

and just incase, here the CSS

#container {
  height:417px;
  width: 320px;
  font-family: Helvetica, Arial, sans-serif;
  color: #FFF;
}
#scroll_container {
  height:417px;
  width: 320px;
  background-color: #0FF;
}
#index_navigation {
  background-color: #EC008C;
  width: 320px;
}
#navigation {
  background-color: #EC008C;
  width: 320px;
}
#nav_buttons {
  height: 60px;
  width: 320px;
  background-image: url(images/buttons/pink_button.svg);
  font-family: Helvetica, Arial, sans-serif;
  font-size: 22px;
  line-height: 60px;
  color: #FFF;
  text-align: center;
}
#header {
  background-color: #9CF;
  height: 177px;
  width: 320px;
}

Thanks

Stefan
  • 5,644
  • 4
  • 24
  • 31
Dan
  • 1,565
  • 3
  • 23
  • 43
  • 2
    Notice that your HTML contains multiple elements with ID "navigation". – Stefan Feb 16 '12 at 12:18
  • A note to Stefan's comment: in a HTML document, you can only use an ID once. It is a unique identifier. Always validate your HTML, it will fix a lot of your problems. – kapa Feb 17 '12 at 10:08

1 Answers1

0

Never used the Cycle plugin but according to the documentation and demos available you can go to a specific slide by calling .cycle(index).

$('.index_navigation_1').click(function() { 
    $('#container').cycle(1); 
    return false; 
}); 

Have a closer look at 'goto slide' demo and Another 'goto' demo (aka: Poor-man's pager).

UPDATE

You can make use of index() to get the position of the first element within the jQuery object relative to its sibling elements.

$('#container').find('#about_us_container').index()

Something even more complicated than typing the index yourself is to create links with classes and a separate click event handler for each link and slide.

You should try something like this instead;

$('.goToSlide').click(function(e) {
  e.preventDefault();
  var selector = $(this).data('slide');
  var slideIndex = $(selector).index();
  $('#container').cycle(slideIndex);
});

together with;

<a href="#" class="goToSlide" data-slide="#about_us_container">Goto #about_us_container</a>
<a href="#" class="goToSlide" data-slide="#contact_us_container">Goto #contact_us_container</a>

Notice that I wrote this example to illustrate the concept and that it hasn´t been tested.

UPDATE 2

Make sure you´re using valid HTML. Start with a basic setup and advance from that.

$('#slide_containers').cycle({
    fx: 'scrollHorz',
    speed: 'fast',
    timeout: 0
});

$('.goToSlide').click(function(e) { // Bind click event to all elements with the class goToSlide
  e.preventDefault();
  var selector = $(this).data('slide'); // Get the data-slide attribute value
  //console.log(selector, $(selector), $(selector).index()); // DEBUG
  var slideIndex = $(selector).index(); // Get the index of the slide element
  $('#slide_containers').cycle(slideIndex); // Cycle to slide by index
});​

See my jsFiddle demo.

Stefan
  • 5,644
  • 4
  • 24
  • 31
  • Hi thanks for the reply but i had already looked at them, i would rather use a #div instead of (1) as i've got a few divs and it will just get confusing later on down the line, i'm sure it can be done but i just need a little help. – Dan Feb 16 '12 at 16:04
  • Hi Stefan, thank again for replying. Sorry for the delay could not login on the laptop, just a red bar along the top stopping me. i've looked at your code and unless it's a copy and paste job i find it very hard to understand, the way i've been learning is already having something work, and then i keep tweaking it till i get the outcome i want, this way i've been learning. I understand your code a little but unless it's a working version i'm still stuck. I've copy and pasted your code in and played about with things but i'm still not getting anywhere – Dan Feb 17 '12 at 15:13
  • @Dan you are using invalid HTML with duplicate `ID`s and the settings object you´re passing to cycle are invalid as it contains a `,` to much at the end. Updated my answer with a working demo. – Stefan Feb 18 '12 at 08:23
  • that code works great thanks, i've got just one little issue, how do i get it to use the url i.e. blahblah.com/#about_us_content i would like to use the back button on the brwser and this is the only way i can think of using it? – Dan Feb 20 '12 at 17:04
  • You could use `window.location.hash`. See http://stackoverflow.com/a/680865/896341 – Stefan Feb 20 '12 at 21:26
  • Hi Stefan, i tried putting in the window.location.hash like the link but everything i do stops it working, i know i'm asking a lot but can you please give me a working demo, i'm starting to understand it more but still not good enough. Thanks for your help so far. – Dan Feb 22 '12 at 16:55
  • Can someone please help me with this, like i say i'm new and need a little direction. – Dan Feb 26 '12 at 16:28