简体   繁体   English

JS从HOVER到ONLOAD

[英]JS from HOVER to ONLOAD

i'm trying to edit some existing code to fulfill my need. 我正在尝试编辑一些现有代码以满足我的需求。

https://tympanus.net/codrops/2010/02/08/awesome-css3-jquery-slide-out-button/ https://tympanus.net/codrops/2010/02/08/awesome-css3-jquery-slide-out-button/

It's effect of sliding out content after you hover over it with the mouse is what i am after. 将鼠标悬停在鼠标上之后滑出内容的效果就是我所追求的。 Seems simple enough, but i would rather have it on a (automatic) timer. 看起来很简单,但我宁愿在(自动)计时器上使用它。 Or rather have no interaction from the user at all for it to work. 或者更确切地说,根本没有来自用户的交互以使其工作。

Let's say it starts closed, then opens up after 2 seconds. 假设它开始关闭,然后在2秒后打开。 Stays open voor 5 seconds and then closes again. 保持打开5秒钟,然后再次关闭。 All without using the mouse to activate it. 全部不使用鼠标来激活它。

<script type="text/javascript">
    $(function() {
        $('.slidebttn').hover(
            function open() {
                var $this       = $(this);
                var $slidelem   = $this.prev();
                $slidelem.stop().animate({'width':'225px'},800);
                $slidelem.find('span').stop(true,true).fadeIn();
                $this.addClass('button_c');
            },
            function close() {
                var $this       = $(this);
                var $slidelem   = $this.prev();
                $slidelem.stop().animate({'width':'70px'},700);
                $slidelem.find('span').stop(true,true).fadeOut();
                $this.removeClass('button_c');
            }
        );
    });
</script>

Any tips on what i need to edit to reach my goal? 有关我需要编辑以达到目标的任何提示?

JSFiddle: https://jsfiddle.net/mj7yumfw/14/ JSFiddle: https ://jsfiddle.net/mj7yumfw/14/

Try this one: 试试这个:

<script type="text/javascript">
$(function() {
    setTimout(initSlider, 2000);
});
function initSlider() {
    open();
    setTimeout(close, 5000);
}
function open() {
    var $this       = $('.slidebttn');
    var $slidelem   = $this.prev();
    $slidelem.stop().animate({'width':'225px'},800);
    $slidelem.find('span').stop(true,true).fadeIn();
    $this.addClass('button_c');
}
function close() {
    var $this       = $('.slidebttn');
    var $slidelem   = $this.prev();
    $slidelem.stop().animate({'width':'70px'},700);
    $slidelem.find('span').stop(true,true).fadeOut();
    $this.removeClass('button_c');
}
</script>

What is different here is that it's using timeout functions to initialize and close slider. 这里的不同之处在于它使用超时函数来初始化和关闭滑块。 These two function (initialize and close) are separated, so you can use them whenever you want. 这两个函数(初始化和关闭)是分开的,因此您可以随时使用它们。

You can manipulate the time when you're slider opens and closes. 您可以在滑块打开和关闭时操纵时间。 Opening time is 2000 right now, which means (~2s) and closing is 5000 (~5s). 现在开放时间是2000 ,这意味着(~2s),结束时间是5000 (~5s)。

It will work only when the buttons are available as the element to slide is found using .slidebttn element. 它仅在按钮可用时才有效,因为使用.slidebttn元素找到要滑动的元素。

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

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