| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1577 人关注过本帖
标题:JQuery动画效果小插件
只看楼主 加入收藏
冰镇柠檬汁儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:北京
等 级:版主
威 望:120
帖 子:8078
专家分:6657
注 册:2005-11-7
结帖率:98.25%
收藏
 问题点数:0 回复次数:6 
JQuery动画效果小插件
既然大家都说JQuery如何如何好,那我写个小插件,没什么特效,只是在div区域中来回弹来弹去的小动画
以下是HTML,附件是程序调用的图片,二楼是JS脚本

<!DOCTYPE html>

<html lang="en" xmlns="http://www.
<head>
    <meta charset="utf-8" />
    <title></title>
    <style>
        .draw-target { width: 320px; height: 256px; position: relative; float: left; margin: 5px; }
    </style>
    <script src="jquery-1.7.1.min.js"></script>
    <script src="sprite.js"></script>
    <script>
        $(document).ready(function () {
            $('.draw-target').bouncyPlugin({
                number: 5,
                bgColor: '#8ff'
            });
        });
    </script>
</head>
<body>
    <div class="draw-target"></div>
    <div class="draw-target"></div>
    <div class="draw-target"></div>
    <div class="draw-target"></div>
</body>
</html>

图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: position relative style title 图片 
2015-06-05 22:47
冰镇柠檬汁儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:北京
等 级:版主
威 望:120
帖 子:8078
专家分:6657
注 册:2005-11-7
收藏
得分:0 
(function ($) {
    $.fn.bouncyPlugin = function (option) {
        var dhtml = function (params) {
            var width = params.width,
                height = params.height,
                imageWidth = params.imageWidth,
                $element = params.target.append('<div/>').find(':last'),
                eleStyle = $element[0].style;

            $element.css({
                position: 'absolute',
                width: width,
                height: height,
                backgroundImage: 'url(' + params.image + ')'
            });

            var that = {
                draw: function (x, y) {
                    eleStyle.left = x + 'px';
                    eleStyle.top = y + 'px';
                },
                changeImage: function (index) {
                    index *= width;
                    var vset = -Math.floor(index / imageWidth) * height,
                        hset = -index % imageWidth;
                    eleStyle.backgroundPosition = hset + 'px ' + vset + 'px';
                },
                show: function () {
                    eleStyle.display = 'block';
                },
                hide: function () {
                    eleStyle.display = 'none';
                },
                destroy: function () {
                    $element.remove();
                }
            };

            return that;
        };

        var bouncy = function (params) {
            var x = params.x,
                y = params.y,
                xdir = params.xdir,
                ydir = params.ydir,
                xmax = params.xmax,
                ymax = params.ymax,
                animIndex = 0,
                that = dhtml(params);

            that.moveAndDrow = function () {
                x += xdir;
                y += ydir;
                animIndex += xdir > 0 ? 1 : -1;
                animIndex %= 5;
                animIndex += animIndex < 0 ? 5 : 0;

                ((xdir < 0 && x < 0) || (xdir > 0 && x >= xmax)) && (xdir *= -1);
                ((ydir < 0 && y < 0) || (ydir > 0 && y >= ymax)) && (ydir *= -1);

                that.changeImage(animIndex);
                that.draw(x, y);
            };

            return that;
        };

        var bouncyBoss = function (num, target) {
            var bys = [], i = 0;
            for (; i < num; i++) {
                bys.push(bouncy({
                    image: 'img.png',
                    imageWidth: 256,
                    width: 64,
                    height: 64,
                    target: target,
                    x: Math.random() * target.width() - 64,
                    y: Math.random() * target.height() - 64,
                    xdir: Math.random() * 4 - 2,
                    ydir: Math.random() * 4 - 2,
                    xmax: target.width() - 64,
                    ymax: target.height() - 64
                }));

                (function () {
                    for (var i = 0, len = bys.length; i < len; i++) {
                        bys[i].moveAndDrow();
                    }
                    setTimeout(arguments.callee, 10);
                })();
            }
        };

        option = $.extend({}, $.fn.bouncyPlugin.defaults, option);

        return this.each(function () {
            var target = $(this);
            target.css('background-color', option.bgColor);
            bouncyBoss(option.number, target);
        });
    };

    $.fn.bouncyPlugin.defaults = {
        bgColor: '#f00',
        number: 10
    };
})(jQuery);

[ 本帖最后由 冰镇柠檬汁儿 于 2015-6-5 22:49 编辑 ]

本来无一物,何处惹尘埃
It is empty at all here, Why pm 2.5 is so TMD high!
2015-06-05 22:48
wmf2014
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:216
帖 子:2039
专家分:11273
注 册:2014-12-6
收藏
得分:0 
这该怎么调用?你有两个js文件,我应该把二楼代码放在哪个文件里?没弄出来。

能编个毛线衣吗?
2015-06-06 10:30
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
4楼同问

剑栈风樯各苦辛,别时冰雪到时春
2015-06-06 10:45
冰镇柠檬汁儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:北京
等 级:版主
威 望:120
帖 子:8078
专家分:6657
注 册:2005-11-7
收藏
得分:0 
<script src="jquery-1.7.1.min.js"></script>
<script src="sprite.js"></script>

问的是这两个脚本吗?
第一个是jquery的类库,现在最高版本好像是1.10.2什么的,网上很多可以下载,实在不行百度首页源代码里也有
https://ss0.

第二个就是2楼的代码,保存成一个UTF-8格式的js文件(记事本另存为里面就可以设置),然后放在html同级目录下就可以了

本来无一物,何处惹尘埃
It is empty at all here, Why pm 2.5 is so TMD high!
2015-06-06 12:38
冰镇柠檬汁儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:北京
等 级:版主
威 望:120
帖 子:8078
专家分:6657
注 册:2005-11-7
收藏
得分:0 
另外图片文件是我随便画的一个png图,效果不是很好,你可以随便照着画,256*128像素的,每个小图是64*64的也放在同级目录下就可以了

本来无一物,何处惹尘埃
It is empty at all here, Why pm 2.5 is so TMD high!
2015-06-06 12:41
快速回复:JQuery动画效果小插件
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016064 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved