将按钮活动脚本更改为onLoad效果

时间:2015-02-25 19:48:27

标签: javascript jquery html

我有一个简单的代码可以在点击特定按钮时在网页的左上角显示一个通知框。

我需要代码可以在网页完全准备好并且在相同位置加载时使用相同的计时器显示通知框。

<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link href="http://fonts.googleapis.com/css?family=Raleway:400,300,700" rel="stylesheet" type="text/css">
    <link rel="stylesheet" type="text/css" href="css/normalize.css" />
    <link rel="stylesheet" type="text/css" href="css/demo.css" />
    <link rel="stylesheet" type="text/css" href="css/ns-default.css" />
    <link rel="stylesheet" type="text/css" href="css/ns-style-growl.css" />
    <script src="js/modernizr.custom.js"></script>
    <!--[if IE]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
</head>
<body class="color-9">
<div class="container">
        <!-- Top Navigation -->
        <div class="main clearfix">
            <div class="column">
              <button id="notification-trigger" class="progress-button">
                  <span class="content">Show Notification</span>
                  <span class="progress"></span>
              </button>
          </div>
      </div>
<!-- Related demos --></div><!-- /container -->
    <script src="js/classie.js"></script>
    <script src="js/notificationFx.js"></script>
    <script>
        (function() {
            var bttn = document.getElementById( 'notification-trigger' );

            // make sure..
            bttn.disabled = false;

            bttn.addEventListener( 'click', function() {
                // simulate loading (for demo purposes only)
                classie.add( bttn, 'active' );
                setTimeout( function() {

                    classie.remove( bttn, 'active' );

                    // create the notification
                    var notification = new NotificationFx({
                        message : '<p>Hello there! I\'m a classic notification but I have some elastic jelliness thanks to <a href="http://bouncejs.com/">bounce.js</a>. </p>',
                        layout : 'growl',
                        effect : 'jelly',
                        type : 'notice', // notice, warning, error or success
                        onClose : function() {
                            bttn.disabled = false;
                        }
                    });

                    // show the notification
                    notification.show();

                }, 1200 );

                // disable the button (for demo purposes only)
                this.disabled = true;
            } );
        })();
    </script>

如果有人可以帮我修改代码,请提前多多谢谢。 :)

2 个答案:

答案 0 :(得分:1)

感谢您的帮助。

我已经编辑并找到了一种方法,这里编辑的最后一个块:

            (function() {
            var bttn = document.getElementById( 'notification-trigger' );

            // make sure..
            bttn.disabled = false;

            addEventListener('load',function() {


                    // create the notification
                    var notification = new NotificationFx({
                        message : '<p>Hello there! I\'m a classic notification but I have some elastic jelliness thanks to <a href="http://bouncejs.com/">bounce.js</a>. </p>',
                        layout : 'growl',
                        effect : 'jelly',
                        type : 'notice', // notice, warning, error or success
                        onClose : function() {
                            bttn.disabled = false;
                        }
                    });

                    // show the notification
                    notification.show();

                }, 1200 );

                // disable the button (for demo purposes only)
                this.disabled = true;

        })();

感谢您的帮助:) 现在工作正常。

答案 1 :(得分:0)

只需单独声明单击处理函数,然后绑定它就可以调用它,而不会触发bttn.click()将执行的单击事件。

// Declare handler:
var clickHandler = function (event) {
  // Your onclick function
};

bttn.addEventListener( 'click', clickHandler); // Bind handler
clickHandler(); // Fire it immediately