JQuery animate and slidetoggle are very buggy and glitch often.
Hey people.
I am new to JQuery, I've only done a little dabbling in the past and am now trying to build a larger program. I want to eventually turn this into a wordpress plugin but this post is not about that.
I am using slidetoggle to cause an area to show up upon certain buttons being pressed and I am using animate to make sure the area that is shown is in the main focus, this is because before I used animate the area would come up due to the slidetoggle but half of the area would be off screen, not very professional.
I am noticing an issue however:
1 - Click on the image, the rise is fine, but the second time it's clicked the close is rather abrupt. (the animation goes right to the top of the page, make it not smooth at all)
2 - In general things still seem rather jaggy.
Am I doing something wrong?
The live link:
- $(document).ready(function(){
- //$('.slider_content').hide();
- var toggle = 0;
- var open = false;
- //Row one
- $("#hide_panel1").click(function(){
- $("#toggle1").trigger('click');
- });
-
- $("#toggle1").click(function(){
- if(toggle != 1 && open == true){
- open = false;
- var panel = "#panel"+toggle;
- $(panel).slideToggle("slow");
- }
- toggle = 1;
- $("#panel1").slideToggle("slow",function(){
-
- $('html, body').animate({
- scrollTop: $("#panel1").offset().top
- }, 2000);});
-
- if(open == true)
- open = false;
- else
- open = true;
- });
- $("#toggle2").click(function(){
- if(toggle != 2 && open == true){
- open = false;
- var panel = "#panel"+toggle;
- $(panel).slideToggle("slow");
- }
- toggle = 2;
- $("#panel2").slideToggle("slow",function(){
-
- $('html, body').animate({
- scrollTop: $("#panel2").offset().top
- }, 2000);});
-
- if(open == true)
- open = false;
- else
- open = true;
- });
- //Row two
- $("#toggle3").click(function(){
- if(toggle != 3 && open == true){
- open = false;
- var panel = "#panel"+toggle;
- $(panel).slideToggle("slow");
- }
- toggle = 3;
- $("#panel3").slideToggle("slow",function(){
-
- $('html, body').animate({
- scrollTop: $("#panel3").offset().top
- }, 2000);});
-
-
- if(open == true)
- open = false;
- else
- open = true;
- });
- $("#toggle4").click(function(){
- if(toggle != 4 && open == true){
- open = false;
- var panel = "#panel"+toggle;
- $(panel).slideToggle("slow");
- }
- toggle = 4;
- $("#panel4").slideToggle("slow",function(){
-
- $('html, body').animate({
- scrollTop: $("#panel4").offset().top
- }, 2000);});
-
-
- if(open == true)
- open = false;
- else
- open = true;
- });
- });
HTML
- <html>
- <head>
- <title>Demo</title>
- <link rel="stylesheet" type="text/css" href="styles.css">
- <script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
- <script src="vertslider.js"></script>
- </head>
-
- <body>
- <div class="main">
- <div class="title">
- <h1>Demo for slider</h1>
- <h2>We are just using JQuery and HTML for this demo.</h2>
- </div>
-
- <div class="vertical_slider">
- <!-- The first row of panels -->
- <div class="slider_content" id="panel1">
- <a href="#" id="hide_panel1">
- <div class="hide_button">
- <h2>HIDE</h2>
- </div>
- </a>
- <div class="fat_col">
- </div>
- <div class="slim_col">
- <p class="default_text">This is some content for item 1</p>
- </div>
- </div>
- <div class="slider_content" id="panel2">
- <div class="fat_col">
- </div>
- <div class="slim_col">
- <p>This is some content for item 2</p>
- </div>
- </div>
- <!-- The first row of testimonial items -->
- <div class="row">
-
- <a href="#" id="toggle1">
- <div class="item" id="img1"></div>
- </a>
- <a href="#" id="toggle2">
- <div class="item" id="img2"></div>
- </a>
- </div>
- <!-- The second row of panels -->
- <div class="slider_content" id="panel3">
- <div class="fat_col">
- </div>
- <div class="slim_col">
- <p>This is some content for item 3</p>
- </div>
- </div>
- <div class="slider_content" id="panel4">
- <div class="fat_col">
- </div>
- <div class="slim_col">
- <p>This is some content for item 4</p>
- </div>
- </div>
- <!-- The second row of testimonial items -->
- <div class="row">
-
- <a href="#" id="toggle3">
- <div class="item" id="img3"></div>
- </a>
- <a href="#" id="toggle4">
- <div class="item" id="img4"></div>
- </a>
- </div>
-
- </div>
-
- </div>
- </body>
- </html>
CSS