2010-04-08 90 views
2

我试图使用JQuery切换功能,但无法正确使用。它不是上下滑动,而是非常快速,而不是以动画方式。
我要实现我的代码的滑动效果,像this已经(请参阅网站设计,重新设计服务滑块):JQuery Help:toggle()无法正常工作

这里是我的代码:

HTML:

<div> 
    <div class="jquery_inner_mid"> 
      <div class="main_heading"> 
       <a href="#"> 
        <img src="features.jpg" alt="" title="" border="0" /></a> 
      </div> 

      <div class="plus_sign"> 
       <img id="imgFeaturesEx" src="images/plus.jpg" alt="" title="" border="0" /> 
       <img id="imgFeaturesCol" src="images/minus.jpg" alt="" title="" border="0" /></div> 
      <div class="toggle_container"> 
       <div id="divMain" > 
       </div> 
      </div> 
     </div> 
     <div class="jquery_inner_mid"> 
      <div class="main_heading"> 
       <img src="About.jpg" alt="" title="" /></div> 
      <div class="plus_sign"> 
       <img id="imgTechnoEx" src="images/plus.jpg" alt="" title="" border="0" /> 
       <img id="imgTechnoCol" src="images/minus.jpg" alt="" title="" border="0" /></div> 
      <div class="toggle_container"> 
       <div id="divTechnossus" > 
       </div> 
      </div> 
     </div> 
    </div> 

JQuery的:

$(function() { 

      document.getElementById('imgFeaturesCol').style.display = 'none'; 
      document.getElementById('imgTechnoCol').style.display = 'none'; 


      $('#imgFeaturesEx').click(function() { 


       $.getJSON("/Visitor/GetFeatureInfo", null, function(strInfo) { 
        document.getElementById('divMain').innerHTML = strInfo; 
       }); 
       $("#divMain").toggle("slow"); 
       document.getElementById('imgFeaturesEx').style.display = 'none'; 
       document.getElementById('imgFeaturesCol').style.display = 'block'; 
      }); 
      $('#imgFeaturesCol').click(function() { 
       document.getElementById('divMain').innerHTML = ""; 
       $("#divMain").toggle("slow"); 
       document.getElementById('imgFeaturesCol').style.display = 'none'; 
       document.getElementById('imgFeaturesEx').style.display = 'block'; 
      }); 

      $('#imgTechnoEx').click(function() { 
       $.getJSON("/Visitor/GetTechnossusInfo", null, function(strInfo) { 
        document.getElementById('divTechnossus').innerHTML = strInfo; 
       }); 
       $("#divTechnossus").slideToggle("slow"); 
       document.getElementById('imgTechnoEx').style.display = 'none'; 
       document.getElementById('imgTechnoCol').style.display = 'block'; 
      }); 
      $('#imgTechnoCol').click(function() { 
       document.getElementById('divTechnossus').innerHTML = ""; 
       $("#divTechnossus").slideToggle("slow"); 
       document.getElementById('imgTechnoCol').style.display = 'none'; 
       document.getElementById('imgTechnoEx').style.display = 'block'; 
      }); 
    }); 

编辑:我也想优化这个代码(因为代码不是很干净+行数也可能是r得出)。我不知道JQuery的正确编码标准。我在JQuery中很新,所以请告诉我正确的路径,以便我可以优化这个愚蠢的代码。

+8

哇,我从来没有见过用'jQuery'包含在我生命中的'getElementById'这么多的调用。 – 2010-04-08 18:42:10

+1

您应该真正将DOM元素缓存在变量中......这一定非常慢。 – 2010-04-08 18:43:24

+0

@Coronatus:我真的不知道这件事(缓存var元件中的DOM元素)。你能否给我解释一下,或者给我提供一个关于这个概念的好链接。这样我就不会再犯这个错误了。谢谢。 – 2010-04-08 18:53:36

回答

5

我无法抗拒你的优化使用代码jQuery

我一直困惑不已,为什么你不得不在那里所有的getElementById来电时,你已经包括jQuery的

试试这个:

(function() { 
     $([ '#imgFeaturesCol', '#imgTechnoCol' ]).hide(); 

     $('#imgFeaturesEx').click(function() { 
      $.getJSON("/Visitor/GetFeatureInfo", null, function(strInfo) { 
       $('#divMain').html(strInfo) 
           .slideToggle("slow"); 
      }); 
      $('#imgFeaturesEx').hide(); 
      $('#imgFeaturesCol').show(); 
     }); 
     $('#imgFeaturesCol').click(function() { 
      $('#divMain').html("") 
          .slideToggle("slow"); 
      $('#imgFeaturesCol').hide(); 
      $('#imgFeaturesEx').show(); 
     }); 

     $('#imgTechnoEx').click(function() { 
      $.getJSON("/Visitor/GetTechnossusInfo", null, function(strInfo) { 
       $('#divTechnossus').html(strInfo) 
            .slideToggle("slow"); 
      }); 
      $('#imgTechnoEx').hide(); 
      $('#imgTechnoCol').show(); 
     }); 
     $('#imgTechnoCol').click(function() { 
      $('#divTechnossus').html("") 
           .slideToggle("slow"); 
      $('#imgTechnoCol').hide(); 
      $('#imgTechnoEx').show();    
     }); 
})(); 
+0

感谢您的帮助雅各布,但这是不正常工作。我的div在滑动按钮上滑动,但在此之后,数据div突然消失。下滑的手段是完美的,但div(其中数据正在加载,这里#divMain)在此之后不可见。 – 2010-04-08 19:17:36

+0

@John,你在说哪个按钮? – 2010-04-08 19:25:13

+0

关于imgFeaturesEx点击(即在$('#imgFeaturesEx')。点击()) – 2010-04-09 04:09:53

2

使用slideToggle('slow');这一翻译的toggle();

+0

还考虑将代码的slideToggle()部分放入$ .getJSON()的回调函数中。 – sepehr 2010-04-08 18:50:24

0

雅各布Relkin给你一个很好的选择,你的代码,我借此机会给这个线程的观众一些规则,从javascript编码到jquery编码的通道:

1-replace all document.getElementById('element') by $('element')

2,所有的CSS样式,您使用的功能$('element').css("name","value");

3为隐藏,并显示出相同的元素,你可以使用toggleslideToggle功能和他们的影响(慢,快.. )