gpt4 book ai didi

javascript - 如何将 svg 按钮变成下载按钮?

转载 作者:太空宇宙 更新时间:2023-11-04 01:10:51 24 4
gpt4 key购买 nike

无论如何,是否可以将已经预制的 SVG 按钮制作成下载按钮。目的是单击按钮,制作动画,然后下载 pdf。我试图将按钮包装在 <a href> 中标签,但这只是导致一个不可见的区域点击进入网站弹出。我尝试应用 download也归于它,但我不熟悉它。

按钮的代码如下。

var path = document.getElementById("path");
var arrow = document.getElementById("arrow");
var base = document.getElementById("base");
var baseTop = base.getBBox().y - parseInt(window.getComputedStyle(base).getPropertyValue("stroke-width"))/2;
var morphTo = "m13,102 h15.2 c2.4,0 4.8,0.9 6.6,2.5 l8.5,7.6 c3.8,3.4 9.5,3.4 13.3,0 l8.5-7.6 c1.8-1.6 4.2-2.5 6.6-2.5 h15.2".match(/([MLHVCSQTA]+)|(-?[\d\.]+)z?/gi);
var morphFrom = base.getAttribute("d").match(/([MLHVCSQTA]+)|(-?[\d\.]+)z?/gi);


function getPathPosition(counter, path) {
return path.getPointAtLength(path.getTotalLength() * counter).y;
}

function movePath(element, path, amount) {
element.setAttribute("transform", "translate(0," + (getPathPosition(amount, path) - path.getBBox().y) +")");
}

function morph(element, from, to, amount) {
if (from.length !== to.length) {
console.error("From path and to path need to have the same number of points");
}
var adjusted = to.map(function(item, i){
return (isNaN(item) || item === from[i]) ? item : item * amount;
});
element.setAttribute("d",adjusted.join(" "));
}

function animate(t, counter, counterBase, reverse, increment, incrementBase) {

if (getPathPosition(counter, path) >= baseTop && incrementBase === 0) {
incrementBase = increment/(1 - counter);
}

if (counter >= 1) {
reverse = true;
} else if ( reverse && counter <= 0 ) {
return false;
}

if (reverse) {
counter -= increment;
counterBase -= incrementBase;
} else {
counter += increment;
counterBase += incrementBase;
}

if (counterBase >= 0) {
morph(base, morphFrom, morphTo, counterBase);
}

movePath(arrow, path, counter);
requestAnimationFrame(function(){
animate(t, counter, counterBase, reverse, increment, incrementBase);
});
}


document.getElementById("download").addEventListener("click", function() {
animate(0, 0, 0, false, 0.08, 0)
});
.download-container {
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.download-btn {
width: 50px;
}

.download-arrow,
.download-base {
fill:none;
stroke-width:10;
stroke-linecap:round;
}
.download-base {
stroke:rebeccapurple;
}
.download-arrow {
stroke:black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="download-container">
<svg version="1.1" id="download" class="download-btn" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 125" style="enable-background:new 0 0 100 125;" xml:space="preserve">

<path id="path" d="M80,85 L80,110" />
<path d="m13,102 h15.2 c2.4,0 4.8,0 6.6,0 l8.5,0 c3.8,0 9.5,0 13.3,0 l8.5,0 c1.8,0 4.2,0 6.6,0 h15.2" id="base" class="download-base"/>

<g id="arrow" class="download-arrow"><line x1="50" y1="20.4" x2="50" y2="80.6"/><line x1="50" y1="80.6" x2="71.4" y2="59.2"/><line x1="50" y1="80.6" x2="28.6" y2="59.2"/></g>
</svg>

最佳答案

在事件动画后面添加window.location.href

document.getElementById("download").addEventListener("click", function() {
animate(0, 0, 0, false, 0.08, 0);
window.location.href="http://www.google.com";//Your download URL
});

关于javascript - 如何将 svg 按钮变成下载按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51229469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com