【问题标题】:Find position of an SVG with jQuery .offset() not working in safari使用 jQuery .offset() 查找 SVG 的位置在 safari 中不起作用
【发布时间】:2022-01-08 19:40:27
【问题描述】:

这段代码在 chrome 中运行良好

    $("#el").hover(function () {
    let position = $(this).offset();
    $('#popover').css(position);
    });

但是 offset() 总是在 safari 中返回这个:

{top: 0, left: 0}

#el 是一个 svg 圆

<circle id="el"  cx="500" cy="153" r="50"></circle>

这是 jQuery 的问题吗?

工作示例:https://jsfiddle.net/82syz54m/1/

在 chrome 中:

在 Safari 中:

【问题讨论】:

标签: jquery svg offset


【解决方案1】:

以下代码兼容所有浏览器:

$("#el").hover(function () {
const circle = document.getElementById('el').getBoundingClientRect();
const position = { left: circle.left, top: circle.top };
$('#popover').css(position);
console.log(position)
});

【讨论】:

    猜你喜欢
    • 2019-07-14
    • 1970-01-01
    • 2017-10-13
    • 2020-07-13
    • 1970-01-01
    • 2016-06-17
    • 2018-12-26
    • 2017-05-25
    • 2017-01-12
    相关资源
    最近更新 更多