简体   繁体   English

将计时器设置为向下滑动,然后在向上滑动后更改按钮

[英]set timer to slidedown and the button change after slideup

What should I put as after I surf the website and in 3 seconds later the ads will slide down from the top of the webpage, and the button name on that div will change to 'hide'. 浏览网站后,我该怎么办?三秒钟后,广告将从网页顶部向下滑动,该div上的按钮名称将变为“隐藏”。 And after we click hide button, the ads will slide up and the button name back again to 'show'. 然后,我们单击“隐藏”按钮,广告将向上滑动,并且按钮名称再次变回“显示”。

It's working for sliding down and sliding up and also the button HIDE and SHOW change successfully. 它可以上下滑动,并且“隐藏”和“显示”按钮也可以成功更改。 But after the button changed back to 'SHOW' again. 但是在按钮再次变回“ SHOW”之后。 I clicked 'SHOW' button but it wont change back to 'HIDE'. 我单击了“显示”按钮,但它不会变回“隐藏”。 and also i didn't set timer to slide down. 而且我也没有设置计时器向下滑动。 because I don't know what to put as I can't find any answer on other questions. 因为我找不到其他问题的答案,所以我不知道该说些什么。

This is my script for JavaScript and jQuery 这是我的JavaScript和jQuery脚本

$(document).ready(function () {
    $(".showAds").click(function () {
    $("#ads").slideToggle("slow");
    $('a#showLink').text('SHOW');
     });
});

This is what I want to show and hide 这就是我要显示和隐藏的

<div id="top">
    <div id="adsBox">
         <div id="ads"> <img src="assets/images/ads.jpg" alt="ads"> </div>
         <p> <a href="#" id="showLink" class="showAds">HIDE</a> </p>
    </div>
</div>

And this is my CSS 这是我的CSS

#adsBox { display:none };

and also i tried something like this. 我也尝试过这样的事情。 And it's still not working too. 而且它仍然无法正常工作。

$(".showAds").click(function () {
    $("#ads").slideUp("slow");
    $('a#showLink').text('SHOW');
    $("#ads").slideDown("slow");
    $('a#showLink').text('HIDE');
});

I know my coding is quite messy. 我知道我的编码很乱。 Please guide me to the right direction. 请引导我朝正确的方向。 And sorry for my bad english. 对不起,我的英语不好。 Thanks. 谢谢。

The button label isn't changing back because, in short, you haven't told it to. 按钮标签不会变回原状,因为总之,您还没有告诉过它。 Your call to slideToggle() switches the visibility of the element back and forth, but your call to .text("SHOW") only does one thing...sets the text to 'SHOW', every time. 您对slideToggle()调用slideToggle()切换了元素的可见性,但是您对.text("SHOW")调用仅做一件事...每次都将文本设置为'SHOW'。

Try something like this: 尝试这样的事情:

$(".showAds").click(function () {
   $("#ads").slideToggle("slow");
   $('a#showLink').text(text == "SHOW" ? "HIDE" : "SHOW");
});

Which is to say - 'if the text is SHOW, set it to HIDE. 就是说-'如果文本为SHOW,则将其设置为HIDE。 If not, set it to SHOW'. 如果不是,请将其设置为SHOW'。

For your timer, use setTimeout() : 对于您的计时器,请使用setTimeout()

setTimeout(function(){ $("ads").slideToggle("slow");}, 3000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM