【问题标题】:adding preloader to div tag将预加载器添加到 div 标签
【发布时间】:2012-06-06 16:50:36
【问题描述】:

我们有一个 ajax 脚本,它可以获取一个单词并将一些相关数据加载到一个 div 标签中。

它工作正常,但我们想将预加载器(不带 jQuery)添加到 div 标签,我们尝试了spin.js,但它只适用于整个页面,这就是我们所做的:(总结)

<head>
<script src="spin.min.js"></script>
</head>

<body>
 <div id="data">

 //data loads here

 </div>

<script type="text/javascript" charset="utf-8">
var opts = {
  lines: 13, // The number of lines to draw
  length: 7, // The length of each line
  width: 4, // The line thickness
  radius: 28, // The radius of the inner circle
  rotate: 0, // The rotation offset
  color: '#000', // #rgb or #rrggbb
  speed: 1, // Rounds per second
  trail: 92, // Afterglow percentage
  shadow: false, // Whether to render a shadow
  hwaccel: false, // Whether to use hardware acceleration
  className: 'spinner', // The CSS class to assign to the spinner
  zIndex: 2e9, // The z-index (defaults to 2000000000)
  top: 'auto', // Top position relative to parent in px
  left: 'auto' // Left position relative to parent in px
};
var target = document.getElementById('data');
var spinner = new Spinner(opts).spin(target);
</script>


</body>

怎么了? 以及任何其他(和更好的)解决方案?

【问题讨论】:

    标签: javascript html ajax preloader


    【解决方案1】:

    您的data div 具有全宽,spin.js 会将图像垂直和水平放置在元素的中心,这就是您在全屏上看到它的原因。

    例如

    <head>
        <script src="spin.js"></script>
    </head>
    <body>
        <div id="data" style="width: 100px; height: 100px; float: left;">
            <!-- Data Loads Here-->
        </div>
        <div id="NoData" style="width: 100px; height: 100px; float: left;">
            Hello Hi Bye
        </div>
        <script type="text/javascript" charset="utf-8">
            var opts = {
                lines : 13, // The number of lines to draw
                length : 7, // The length of each line
                width : 4, // The line thickness
                radius : 28, // The radius of the inner circle
                rotate : 0, // The rotation offset
                color : '#000', // #rgb or #rrggbb
                speed : 1, // Rounds per second
                trail : 92, // Afterglow percentage
                shadow : false, // Whether to render a shadow
                hwaccel : false, // Whether to use hardware acceleration
                className : 'spinner', // The CSS class to assign to the spinner
                zIndex : 2e9, // The z-index (defaults to 2000000000)
                top : 'auto', // Top position relative to parent in px
                left : 'auto' // Left position relative to parent in px
            };
            var target = document.getElementById('data');
            var spinner = new Spinner(opts).spin(target);
        </script>
    </body>
    

    【讨论】:

    • 但是:1 - 旋转工作直到整个页面加载(图片、徽标等) 2 - 在测试 ajax 时它加载数据而没有任何加载飞溅。
    • 你是如何在 ajax start 上初始化你的 spin 的?
    • 似乎是重点..怎么做? (我是 ajax/loader 处理的菜鸟)
    • 可以使用jQuery ajaxStart, ajaxStop 吗?
    • 请在 spin.js 之前使用 ,我也注意到他们的网站上有一个 jQuery 插件。看这个discussion如何使用ajaxStart、ajaxStop。是的,如果你使用 jQuery,它会比普通的 javascript 容易得多
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    相关资源
    最近更新 更多