首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用圆形彩色动画填充div

用圆形彩色动画填充div
EN

Stack Overflow用户
提问于 2014-07-21 23:29:48
回答 1查看 1.4K关注 0票数 1

在我的主页上,我想要创建一个div,其中的使用可以点击改变背景色。要改变这一点,我想使用这样的动画:点击后,新的颜色应该在鼠标周围展开圆形,点击并用新的颜色填充整个div。

为此,我必须正确地使用JavaScript,或者只能使用CSS才能这样做?当然,我知道如何在EventListener中设置JavaScript,但是我不知道如何用新的颜色填充div循环。你知道怎么做吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-22 00:14:11

这可能就是你要找的东西:http://jsfiddle.net/F9pLC/

其想法是使用border-radius创建一个圆圈,然后使其绝对定位。然后我们就可以用鼠标坐标来生长它。

代码语言:javascript
运行
复制
// You can modify this if you want
var START_RADIUS = 25;
$("#someText").click(function (e) {
    // Get the width and the height
    var width = $(this).width(),
        height = $(this).height();
    // Get the diagonal (this is what our circle needs to expand to)
    var diag = Math.ceil(Math.sqrt(width * width + height * height)) * 2;
    // Get the mouse coordinates
    var pageX = e.pageX,
        pageY = e.pageY;
    // Create a circle
    $('<div class="circle">').appendTo("#someText").css({
        width: START_RADIUS * 2,
        height: START_RADIUS * 2,
        "border-radius": START_RADIUS,
        top: pageY - START_RADIUS,
        left: pageX - START_RADIUS,
        "background-color": $("#newColor").val()
    }).animate({
        width: diag,
        height: diag
    }, {
        step: function (now, fx) {
            // This occurs every step of the animation
            // Modify the radius so that it grows along with the width and height
            if (fx.prop === "height") return;
            $(this)
                .css("top", pageY - START_RADIUS - now / 2)
                .css("left", pageX - START_RADIUS - now / 2)
                .css("border-radius", now / 2);
        },
        easing: "linear",
        duration: 2000, // The number of milis to grow completely
        done: function () {
            // Remove the circle and change the background color
            $("#someText").css("background-color", $(this).css("background-color")).css("z-index", "");
            $(this).remove();
        }
    });
    $("#someText").css("z-index", -3);
});
// So that when we click on the color input, it doesn't create a circle
$("#newColor").click(function(e) { e.stopPropagation(); });
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24876527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档