animate点击按钮变大并以元素中心向外变大

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .box{
            width:100px;
            height:100px;
            margin:200px 200px;
            border-radius:10px;
            box-shadow: 0 0 20px #999;
            line-height: 300px;
            text-align: center;
            font-size: 50px;
            color:#fff;
        }
        #box1{
            background:#5298c7;
        }
    </style>
</head>
<body>
<div id="box1" class="box"></div>
<script src="../js/jquery-1.12.4.js"></script>
<script>
    $('#box1').click(function () {
        var $this=$(this);
        $this.css({
            position:'absolute',
            //获得当前已定位元素的top值
            top:$this.position().top,
            left:$this.position().left
        })
            .animate({
                //获得当前元素的宽度并*2
                width:$this.width()*2,
                height:$this.height()*2,
                top:$this.position().top-$this.height(),
                left:$this.position().left-$this.width()
        },500)
    })
</script>
</body>
</html>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要现到达某个元素旁的动画效果,可以使用 jQuery 的 `animate()` 方法和 `offset()` 方法。`offset()` 方法可以获取元素的位置信息,从而让我们知道动画需要过渡到哪个位置。具体步骤如下: 1. 引入 jQuery 库。 2. 获取目标元素和当前元素。可以使用 `$` 函数、`id` 或其他选择器来获取元素。 3. 使用 `offset()` 方法获取目标元素的位置信息。 4. 使用 `animate()` 方法,让当前元素过渡到目标元素的位置。可以使用对象或字符串的形式来设置多个属性。 5. 设置过渡时间和缓动函数。过渡时间越长,动画的速度就越慢。缓动函数会影响动画的加速度和减速度,从而让动画更加自然。 示例代码如下: HTML ```html <button id="btn">点击我</button> <div id="box1"></div> <div id="box2"></div> ``` CSS ```css #box1 { position: absolute; top: 200px; left: 200px; width: 100px; height: 100px; background-color: red; } #box2 { position: absolute; top: 400px; left: 400px; width: 100px; height: 100px; background-color: blue; } ``` JavaScript ```javascript $('#btn').click(function() { var targetOffset = $('#box2').offset(); $('#box1').animate({ 'top': targetOffset.top, 'left': targetOffset.left }, 1000, 'easeOutElastic'); }); ``` 在 JavaScript 中,我们使用了 jQuery 的 `$` 函数来获取元素。当按钮被点击时,我们使用 `offset()` 方法获取了目标元素的位置信息。然后,我们调用了 `animate()` 方法来让当前元素过渡到目标元素的位置。`animate()` 方法的第一个参数是一个对象,用于设置要过渡的 CSS 属性和终止值。`animate()` 方法的第二个参数是过渡时间,单位是毫秒。第三个参数是缓动函数,可以使用 jQuery 提供的预设函数(如 `easeOutElastic`)或自定义函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值