通过 svg 路径滚动移动图像

2023-12-13

我想通过滚动上的 svg 路径移动对象=) 我试图将滚动上的部分路径添加到路径中,但它仍然不起作用。救命!!!=)https://jsfiddle.net/YuriiBielozertsev/Ltx9ed0L/

<?xml version="1.0"?>
<svg viewBox="0 0 120 120" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink">
    <!-- Draw the outline of the motion path in grey, along with 2 small circles at key points -->
    <path d="M10,110 A120,120 -45 0,1 110 10 A120,120 -45 0,1 10,110" stroke="green" stroke-width="2" fill="none" id="theMotionPath"/>
        <circle cx="10" cy="110" r="3" fill="#000"/>
        <circle cx="110" cy="10" r="3" fill="#000"/>

        <!-- Red circle which will be moved along the motion path. -->
        <circle cx="0" cy="" r="5" fill="red">

        <!-- Define the motion path animation -->
        <animateMotion dur="6s" repeatCount="indefinite">
            <mpath xlink:href="#theMotionPath"/>
        </animateMotion>
    </circle>
</svg>

像这样的东西吗?

这是如何运作的:

  1. 当我们收到滚动事件时,我们:
  2. 计算我们距离页面有多远
  3. 使用以下命令将此百分比转换为路径上的位置<path>元素函数getTotalLength() and getPointAtLength().
  4. 重新定位该点,使其出现在此时。
function positionTheDot() {

  // What percentage down the page are we? 
  var scrollPercentage = (document.documentElement.scrollTop + document.body.scrollTop) / (document.documentElement.scrollHeight - document.documentElement.clientHeight);

  // Get path length
  var path = document.getElementById("theMotionPath");
  var pathLen = path.getTotalLength();
  
  // Get the position of a point at <scrollPercentage> along the path.
  var pt = path.getPointAtLength(scrollPercentage * pathLen);
  
  // Position the red dot at this point
  var dot = document.getElementById("dot");
  dot.setAttribute("transform", "translate("+ pt.x + "," + pt.y + ")");
  
};

// Update dot position when we get a scroll event.
window.addEventListener("scroll", positionTheDot);

// Set the initial position of the dot.
positionTheDot();
.verylong {
  height: 2000px;
}

svg {
  position: fixed;
  width: 200px;
  height: 200px;
}
<svg viewBox="0 0 120 120" xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink">
    <!-- Draw the outline of the motion path in grey, along with 2 small circles at key points -->
    <path d="M10,110 A120,120 -45 0,1 110 10 A120,120 -45 0,1 10,110" stroke="green" stroke-width="2" fill="none" id="theMotionPath"/>
    <circle cx="10" cy="110" r="3" fill="#000"/>
    <circle cx="110" cy="10" r="3" fill="#000"/>

    <!-- Red circle which will be moved along the motion path. -->
    <circle cx="0" cy="0" r="5" fill="red" id="dot"/>
</svg>

<div class="verylong">
</div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过 svg 路径滚动移动图像 的相关文章

随机推荐

  • 为什么使用 boost::copy_exception 时会丢失类型信息?

    当我使用boost copy exception将异常复制到exception ptr 我丢失了类型信息 看一下下面的代码 try throw std runtime error something catch exception e pt
  • 在 .NET Core 中的 MVC 之外使用 Razor

    我想在我用 NET Core 编写的 NET 控制台应用程序中使用 Razor 作为模板引擎 我遇到的独立 Razor 引擎 RazorEngine RazorTemplates 都需要完整的 NET 我正在寻找适用于 NET Core 的
  • Python 3 中“python -m SimpleHTTPServer”的等价物是什么

    Python 3 相当于什么python m SimpleHTTPServer From the docs The SimpleHTTPServer模块已合并到http server在Python 3 0中 将源文件转换为 3 0 时 2t
  • 是否可以在 JavaFX 中制作对角列标题?

    我的 JavaFX TableView 看起来像这样 如果顶部的标签是diagonal以节省空间 我想象它是这样的 目标是使该列与内容紧密贴合 尽管标题标签很长 能做到吗 None
  • Haskell 中的基本 I/O 性能

    另一个微基准测试 为什么是这个 循环 用ghc O2 fllvm 7 4 1 Linux 64位3 2内核 重定向到 dev null mapM print 1 100000000 比普通的简单 for 循环慢大约 5 倍C with wr
  • 为什么 Python 中的整数需要三倍的内存?

    在 64 位系统上 Python 中的整数占用 24 个字节 这是例如所需内存的 3 倍 C 表示 64 位整数 现在 我知道这是因为 Python 整数是对象 但是额外的内存有什么用呢 我有我的猜测 但很高兴能确定 请记住 Pythoni
  • GroupBy 从 IEnumerable 对象列表中删除重复项

    我有一个重复名称列表 我想获取没有重复名称的列表 CSVCategories from line in File ReadAllLines path Skip 1 let columns line Split select new Cate
  • LiveData 阻止在开始观察时接收最后一个值

    是否可以预防LiveData开始观察时收到最后一个值 我正在考虑使用LiveData作为事件 例如 显示消息 导航事件或对话框触发器等事件 类似于EventBus 与之间的通信相关的问题ViewModel和片段 谷歌给了我们LiveData
  • 涉及 OR 条件的高级索引 (pgsql)

    我开始更好地掌握 PostgreSQL 索引 但我遇到了 OR 条件的问题 我不知道如何优化索引以实现更快的查询 我有 6 个条件 当单独运行时 它们的成本似乎很小 以下是修剪查询的示例 包括查询计划计算时间 注意 为了降低复杂性 我没有输
  • 可以从 STL 容器继承实现而不是委托吗?

    我有一个类 它适应 std vector 来对特定于域的对象的容器进行建模 我想向用户公开大部分 std vector API 以便他们可以在容器上使用熟悉的方法 size clear at 等 和标准算法 这似乎是我的设计中反复出现的模式
  • git 更改提交者姓名和特定提交者电子邮件的所有历史记录

    我如何无法更改我所有提交历史记录的名称和电子邮件 但特定提交者的名称和电子邮件 类似于 foreach allcommits if committer name Hugo Casa 改变 提交者名称 提交者 电子邮件 作者姓名 作者邮箱 这
  • 计算数据帧中连续的数并获取发生这种情况的索引

    我有一个pandas DataFrame具有整数列名称 其中包含零和一 输入示例 12 13 14 15 1 0 0 1 0 2 0 0 1 1 3 1 0 0 1 4 1 1 0 1 5 1 1 1 0 6 0 0 1 0 7 0 0 1
  • 活动窗口上的 Windows 系统事件是否发生变化?

    我正在开发的桌面应用程序需要知道应用程序运行时哪些窗口处于活动状态 目前它执行GetForegroundWindow 呼叫user32 dll 每 250 毫秒一次 该方法不是很准确 是否有任何 Windows WINAPI 事件在每次活动
  • sqlite 查询获取所有表名列表以及其中的记录数

    请帮助解决以下查询 sqlite 查询获取所有表名列表以及其中的记录数 我想获取 Sqlite3 数据库中每个表的行数 我想避免写出手写查询 我可以获得这样的表列表 选择名称 FROM sqlite master WHERE type ta
  • Python:如何设置Python请求的参数

    我有这个可以运行的 python 请求代码 但我不明白参数代表什么 我想了解如何设置 python 请求的参数以及是否有很好的参考 这是我使用的代码 url https www walmart com store 1003 York pa
  • “ASN1 损坏了数据。”替换公钥但不替换私钥时出错

    学习使用 RSA 签名的机制 我有一个可以在下面运行的代码块 var privateRSAKey File ReadAllText RSAPrivateKey txt Trim Regex privateRSAKeyRegex new Re
  • 关于 ruby​​ 中“获取”的问题[重复]

    这个问题在这里已经有答案了 我想知道为什么当我尝试获取不同的输入时 它会忽略我的第二个输入 usr bin env ruby Class Definitions class Animal attr accessor type weight
  • 检测设备会话何时过期

    我想使用 devise 来记录用户会话何时超时 timeoutable 眼下 timeoutable正在按预期工作 并在指定的不活动时间后将用户重定向到登录页面 但除此之外 我们还希望在发生这种情况时进行记录 以帮助优化我们的超时期限 显然
  • Composer 中的自动增量字段

    创建 Asset 实例时是否有可用的 auto increment 字段类型 例如 资产 ID 应该是自动生成的字段 并且应该在运行时创建 而不是在应用程序级别提及该值 我可以通过创建一个交易处理器函数来检查资产列表并添加 1 作为替代方案
  • 通过 svg 路径滚动移动图像

    我想通过滚动上的 svg 路径移动对象 我试图将滚动上的部分路径添加到路径中 但它仍然不起作用 救命 https jsfiddle net YuriiBielozertsev Ltx9ed0L