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

Change button active script to onLoad effect

本文关键字:onLoad 效果 按钮 活动 脚本      更新时间:2024-05-27

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

我需要代码可以在网页完全准备好的那一刻显示在通知框中;在同一个地方,用同一个定时器加载。

<!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>

如果有人能帮我修改代码,请提前感谢。:)

感谢您的帮助。

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

            (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;
        })();

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

只需单独声明点击处理程序函数,然后在绑定后即可调用它,而无需触发点击事件,bttn.click()会这样做。

// Declare handler:
var clickHandler = function (event) {
  // Your onclick function
};
bttn.addEventListener( 'click', clickHandler); // Bind handler
clickHandler(); // Fire it immediately