我有如下代码:【div部分】:<div class="tog_contact"><div class="t_con_box"><div class="map"><img src="images/introduction.jpg" /></div></div></div><div class="tog" id="tog" style="top:620px; "><span>收起,开始查询</span></div>【jquery部分】:$('#tog').toggle(function(){$(this).animate({top:'0px'},320).addClass("tog").removeClass("togclose").html('<span>展开,查看详情</span>');$('.tog_contact').slideUp(320);},function(){$(this).animate({top:'620px'},320).addClass("togclose").removeClass("tog").html('<span>收起,开始查询</span>');$('.tog_contact').slideDown(320);})这个toggle本身是没得问题的,现在的情况是我在前边加了一个延时函数,超过3秒div#tog就会自动收起这时候div#tog的高度已经为零了,再点击#tog的话,它还是自动执行第一个函数,而不是根据实际情况执行第二个函数。请问要怎么样改进才能让它根据情况选择运行哪个函数呢?
2 回答
潇潇雨雨
TA贡献1833条经验 获得超4个赞
你好!
不清楚你的延时函数是怎么写的?使用的是setTimeout()么?
toggle是通过click事件来触发的,这样试试:
$(document).ready(function(){ setTimeout('$("#tog").click()',1000); $('#tog').toggle(function(){ $(this).animate({top:'0px'},320).addClass("tog").removeClass("togclose").html('<span>展开,查看详情</span>'); $('.tog_contact').slideUp(320); },function(){ $(this).animate({top:'620px'},320).addClass("togclose").removeClass("tog").html('<span>收起,开始查询</span>'); $('.tog_contact').slideDown(320); })}); |
心有法竹
TA贡献1866条经验 获得超5个赞
$('#tog').click(function(){ if($(this).attr('class')=='togclose'){ $(this).animate({top:'0px'},320).addClass("tog").removeClass("togclose").html('<span>展开,查看详情</span>'); $('.tog_contact').slideUp(320); } else{ $(this).animate({top:'620px'},320).addClass("togclose").removeClass("tog").html('<span>收起,开始查询</span>'); $('.tog_contact').slideDown(320); }}); |
用toggle反而显得麻烦,不如用if判断一下。
添加回答
举报
0/150
提交
取消
