JQuery animate and slidetoggle are very buggy and glitch often.

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:


JS
  1. $(document).ready(function(){
  2.     //$('.slider_content').hide();
  3.     var toggle = 0;
  4.     var open = false;
  5.     //Row one
  6.     $("#hide_panel1").click(function(){
  7.      $("#toggle1").trigger('click');
  8.     });
  9.     
  10.     $("#toggle1").click(function(){
  11.         if(toggle != 1 && open == true){
  12.             open = false;
  13.             var panel = "#panel"+toggle;
  14.             $(panel).slideToggle("slow");
  15.         }
  16.         toggle = 1;
  17.         $("#panel1").slideToggle("slow",function(){

  18.         $('html, body').animate({
  19.    scrollTop: $("#panel1").offset().top
  20.    }, 2000);});
  21.         if(open == true)
  22.             open = false;
  23.         else
  24.             open = true;
  25.     });
  26.     $("#toggle2").click(function(){
  27.         if(toggle != 2 && open == true){
  28.             open = false;
  29.             var panel = "#panel"+toggle;
  30.             $(panel).slideToggle("slow");
  31.         }
  32.         toggle = 2;
  33.         $("#panel2").slideToggle("slow",function(){

  34.         $('html, body').animate({
  35.    scrollTop: $("#panel2").offset().top
  36.    }, 2000);});
  37.         if(open == true)
  38.             open = false;
  39.         else
  40.             open = true;
  41.     });
  42.     //Row two
  43.     $("#toggle3").click(function(){
  44.         if(toggle != 3 && open == true){
  45.             open = false;
  46.             var panel = "#panel"+toggle;
  47.             $(panel).slideToggle("slow");
  48.         }
  49.         toggle = 3;
  50.         $("#panel3").slideToggle("slow",function(){

  51.         $('html, body').animate({
  52.    scrollTop: $("#panel3").offset().top
  53.    }, 2000);});
  54.         if(open == true)
  55.             open = false;
  56.         else
  57.             open = true;
  58.     });
  59.     $("#toggle4").click(function(){
  60.         if(toggle != 4 && open == true){
  61.             open = false;
  62.             var panel = "#panel"+toggle;
  63.             $(panel).slideToggle("slow");
  64.         }
  65.         toggle = 4;
  66.         $("#panel4").slideToggle("slow",function(){

  67.         $('html, body').animate({
  68.    scrollTop: $("#panel4").offset().top
  69.    }, 2000);});
  70.         if(open == true)
  71.             open = false;
  72.         else
  73.             open = true;
  74.     });
  75. });

HTML

  1. <html>
  2. <head>
  3. <title>Demo</title>
  4. <link rel="stylesheet" type="text/css" href="styles.css">
  5. <script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
  6. <script src="vertslider.js"></script>
  7. </head>

  8. <body>
  9. <div class="main">
  10. <div class="title">
  11. <h1>Demo for slider</h1>
  12. <h2>We are just using JQuery and HTML for this demo.</h2>
  13. </div>
  14. <div class="vertical_slider">
  15. <!-- The first row of panels -->
  16. <div class="slider_content" id="panel1">
  17. <a href="#" id="hide_panel1">
  18. <div class="hide_button">
  19. <h2>HIDE</h2>
  20. </div>
  21. </a>
  22. <div class="fat_col">
  23. </div>
  24. <div class="slim_col">
  25. <p class="default_text">This is some content for item 1</p>
  26. </div>
  27. </div>
  28. <div class="slider_content" id="panel2">
  29. <div class="fat_col">
  30. </div>
  31. <div class="slim_col">
  32. <p>This is some content for item 2</p>
  33. </div>
  34. </div>
  35. <!-- The first row of testimonial items -->
  36. <div class="row">
  37. <a href="#" id="toggle1">
  38. <div class="item" id="img1"></div>
  39. </a>
  40. <a href="#" id="toggle2">
  41. <div class="item" id="img2"></div>
  42. </a>
  43. </div>
  44. <!-- The second row of panels -->
  45. <div class="slider_content" id="panel3">
  46. <div class="fat_col">
  47. </div>
  48. <div class="slim_col">
  49. <p>This is some content for item 3</p>
  50. </div>
  51. </div>
  52. <div class="slider_content" id="panel4">
  53. <div class="fat_col">
  54. </div>
  55. <div class="slim_col">
  56. <p>This is some content for item 4</p>
  57. </div>
  58. </div>
  59. <!-- The second row of testimonial items -->
  60. <div class="row">
  61. <a href="#" id="toggle3">
  62. <div class="item" id="img3"></div>
  63. </a>
  64. <a href="#" id="toggle4">
  65. <div class="item" id="img4"></div>
  66. </a>
  67. </div>
  68. </div>
  69. </div>
  70. </body>
  71. </html>

CSS

  1. body {
  2.     font-family: sans-serif;
  3.     width: 90%;
  4.     margin: 0 auto;
  5. }

  6. #lower1, #lower2 {
  7.     position: relative;
  8.     padding-top: 10px;
  9. }

  10. #bar {
  11.     padding: 10px;
  12.     background-color: #CCCCCC;
  13. }

  14. .slider_content {
  15.     display: none;
  16.     background-color: #cccccc;
  17. margin-left:20px;
  18. margin-right:20px;
  19. margin-top:40px;
  20. margin-bottom:20px;
  21.     width:1000px;
  22.     height:600px;
  23.     
  24. }

  25. #panel1, #panel2, #panel3, #panel4 {
  26.     display: none;
  27.     background-color: #AAAAAA;
  28.     padding: 10px;
  29. }

  30. .fat_col {
  31. width:700px;
  32. }

  33. .slim_col {
  34. width:300px;
  35. }

  36. .hide_button {
  37. width:1000px;
  38. height:30px;
  39. text-align:center;
  40. margin-top:-30px;
  41. }

  42. .title {
  43. padding:30px;
  44. }

  45. .panel {


  46. }

  47. .item {
  48. margin-left:20px;
  49. margin-right:20px;
  50. margin-top:20px;
  51. margin-bottom:20px;
  52. width:500px;
  53. height:300px;
  54. float:left;
  55. }
  56. /* Image backgrounds and their hovers */

  57. #img1 {
  58. background: url('images/img1.png') no-repeat left top;
  59. }

  60. #img1:hover {
  61. background-position: -500px 0px;
  62. }

  63. #img2 {
  64. background: url('images/img2.png') no-repeat left top;
  65. }

  66. #img2:hover {
  67. background-position: -500px 0px;
  68. }

  69. #img3 {
  70. background: url('images/img3.png') no-repeat left top;
  71. }

  72. #img3:hover {
  73. background-position: -500px 0px;
  74. }

  75. #img4 {
  76. background: url('images/img4.png') no-repeat left top;
  77. }

  78. #img4:hover {
  79. background-position: -500px 0px;
  80. }

  81. /* Row controls */

  82. .row {
  83. width:1200px;
  84. height:300px;
  85. }