js实现气泡效果

关键步骤:

1、引入js文件

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src='js/jquery.thoughtBubble.js'></script>

2、在需要使用气泡效果的地方

<div id='mainContainer' class='container'>
   <img src='ahout.JPG' id="thoughtBubble" alt='whats up?' />
</div>

3、使用气泡效果

<script type="text/javascript">
$(window).ready( function() {

   $('#thoughtBubble').thoughtBubble({
      text: 'baby,I love you',
      font: 'avenir'

   });

});

4、这是jquery.thoughtBubblr.js代码

(function($) {

$.fn.thoughtBubble = function( defaults ) {

    var settings = $.extend({

        backgroundColor: 'white',
        fontColor: 'black',
        width: '330px',
        height: '210px',
        fontSize: '15px',
        bubbleColor: 'white',
        speed: 125
        
        }, defaults ),

    getBubbleDiv = function( container ) {

        var offset = container.offset(),
            modifiedHeight = offset.top - parseInt( settings.height ),
            style = '"position: absolute; top:' + modifiedHeight + 'px; left:' + offset.left + 'px ; width:' + settings.width + '; height:' + settings.height + ';"',
            bubbleContainer = "<div class='bubble-holder' style=" + style + ">" + getMainBubble() + getBubbles() + "</div>";

        return bubbleContainer;

    },

    getMainBubble = function() {

        return '<div class="main-bubble-holder"><div class="bubble main-bubble">' + getText() + '</div></div>';

    },

    getText = function() {

        return '<span style="vertical-align: middle; color: ' + settings.fontColor + ';font-size: ' + settings.fontSize + '; font-family: ' + settings.font + '">' + settings.text + '</span>';

    },

    getBubbles = function() {

        return '<div class="sm-bubble-holder"><div class="bubble bubbleLg"></div><div class="bubble bubbleMd"></div><div class="bubble bubbleSm"></div></div>';
    },

    animate = function(){

        var bubbles = $(document).find('.bubble'),
            reversed = bubbles.get().reverse(),
            speed = settings.speed;
        
        $(reversed[0]).stop().animate({ opacity: 1}, speed, function() {
            $(reversed[1]).animate({ opacity: 1}, speed, function() {
                $(reversed[2]).animate({ opacity: 1}, speed, function() {
                    $(reversed[3]).animate({ opacity: 1},speed);
                });
            });
        });

    },

    unanimate = function() {

        var bubbles = $(document).find('.bubble');

        bubbles.stop().animate({opacity: 0});

    },

    shiftDiv = function( container ) {

        var bubbleHolder = $(document).find('.bubble-holder'),
            previousPosition = container.offset().left;

        bubbleHolder.css('left', previousPosition);
    };

    return this.each( function() {

        var $this = $(this),
            container = getBubbleDiv( $this );

        $this.on('mouseenter', animate );

        $this.on('mouseout', unanimate );

        $(window).on('resize', shiftDiv.bind(this, $this) );

        return $this.parent().prepend(container);

    });
    

};

})(jQuery);


转载于:https://www.cnblogs.com/hcxl/p/8321553.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值