我已经创建了SVG光标,我想在鼠标滚动时动态更改光标半径,放大半径,缩小半径。
CSS SVG游标,这有效:
.brushCursor {
cursor: url('data:image/svg+xml;utf8,<svg id="svg" xmlns="http://www.w3.org/2000/svg" version="1.1" width="32" height="32"><circle cx="10" cy="10" r="5" stroke-width="2" style="stroke: black; fill: red;"/></svg>') 10 10, pointer;
}
但是我想动态更改SVG的半径。半径应在1到10的范围内。到目前为止,我做到了,但是光标没有更新:
const brush = document.querySelector('.brush');
const radius = 5;
brush.style.cursor = `url('data:image/svg+xml;utf8,<svg id="svg" xmlns="http://www.w3.org/2000/svg" version="1.1" width="32" height="32"><circle cx="10" cy="10" r=${radius} stroke-width="2" style="stroke: black; fill: red;"/></svg>')`;
答案 0 :(得分:2)
我正在创建一个新的<style>
元素s
,并向其中添加规则。
使用input type='range'
来更改半径。
let r = 3;
let s = document.createElement("style");
document.head.appendChild(s);
changeCursor(r)
test.addEventListener("input", ()=>{
r = test.value;
changeCursor(r)
})
function changeCursor(r){
let rule = `cursor: url('data:image/svg+xml;utf8,<svg id="svg" xmlns="http://www.w3.org/2000/svg" version="1.1" width="32" height="32"><circle cx="10" cy="10" r="${r}" stroke-width="2" style="stroke: black; fill: red;"/></svg>') 10 10, pointer;`
s.textContent = `.brushCursor { ${rule} }`;
}
.brushCursor {
height:100vh;
/*cursor: url('data:image/svg+xml;utf8,<svg id="svg" xmlns="http://www.w3.org/2000/svg" version="1.1" width="32" height="32"><circle cx="10" cy="10" r="3" stroke-width="2" style="stroke: black; fill: red;"/></svg>') 10 10, pointer;*/
}
#test{position:absolute; top:1em;}
<div class="brushCursor"></div>
<input id="test" type="range" min="2" max="7" value="3" />
为了在滚动条上进行更改,我需要了解更多。我正在使用的输入的最大值为7。也许8可以做到;但是更大的半径会给你一个切圆。您到底想如何在滚动上扩大自己的圈子。页面的高度也不同。您打算使用的事件是“滚动”还是“移动”?