首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单击svg元素时应用焦点css

单击svg元素时应用焦点css
EN

Stack Overflow用户
提问于 2019-07-10 14:01:23
回答 2查看 1.6K关注 0票数 1

我有个svg文件。

我会说,当我点击一个车站的名称,站保持规模。

为此,我使用了focus方法,然后在CSS中使用选择器:focused应用效果。

但这不管用,什么都没发生。

ps:你可以忽略forEach循环--理解它不是那么重要

代码语言:javascript
复制
let stops = document.querySelector("#stops");
// all the g elements in the stops 
let gs = stops.querySelectorAll("g");

// for each g in the gs
gs.forEach(g=>{
  // the rectangle in this g element
  let thisRect = g.querySelector("rect");
  // the circle in this g element
  let thisCircle = g.querySelector("circle");
  // the coords of the circle's center used for the transform-origin
  let x = thisCircle.getAttribute("cx");
  let y = thisCircle.getAttribute("cy");
  // the bounding box of the group
  let bb = g.getBBox();
  // set the rect's attributes
  thisRect.setAttributeNS(null, "x", bb.x);
  thisRect.setAttributeNS(null, "y", bb.y);
  thisRect.setAttributeNS(null, "width", bb.width);
  thisRect.setAttributeNS(null, "height", bb.height);
  
  // set the value for the transform-origin for this group
  g.style.transformOrigin =  `${x}px ${y}px`;
  

  
})

  document.getElementById('g3670').focus()
  
  function showmessage() {
  alert("heloo");
  }
代码语言:javascript
复制
text{
  font-family: Lato;
  font-size:16px;
}
g * {pointer-events:none;}
g rect{pointer-events:all;}
#stops g{transform: scale(1);cursor: pointer;}
#stops g:hover {
    transform: scale(2); 
}
#stops g:active {

    transform: scale(2)
}
代码语言:javascript
复制
<svg id="Calque_1" data-name="Calque 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 775.43 469.98">
    <defs>
        <style>.cls-1{fill:none;stroke-width:5px;}.cls-1,.cls-2{stroke:#a15256;}.cls-2{fill:#fff;}.cls-3{isolation:isolate;font-size:42.79px;font-family:ArialMT, Arial;}</style>
    </defs>
    <title>line</title>
    <path id="path7" class="cls-1" d="M202,67.72,329.33,215.86" transform="translate(-200.1 -66.09)" />
    <path id="path8" class="cls-1" d="M329.35,215.87,449,355" transform="translate(-200.1 -66.09)" />
    <path id="path9" class="cls-1" d="M449,355c41.53,51.11,96.22,63.08,117.9,69.28" transform="translate(-200.1 -66.09)" />
    <path id="path10" class="cls-1" d="M566.86,424.29C655.43,460.48,977.38,391.48,973,536" transform="translate(-200.1 -66.09)" />
    <g id="stops">
        <g id="g3670">
            <rect fill="none"/>
            <circle class="cls-2" cx="129.24" cy="149.78" r="13.58" />
            <text id="text3668" class="cls-3" transform="translate(145 160)">Station1</text>
        </g>
        <g id="g3700">
            <rect fill="none"/>
            <circle class="cls-2" cx="248.91" cy="288.93" r="13.58" />
            <text id="text3698" class="cls-3" transform="translate(270 300)">Station2</text>
        </g>
        <g id="g3750">
            <rect fill="none"/>
            <circle class="cls-2" cx="366.75" cy="358.2" r="13.58" />
            <text id="text3748" class="cls-3" transform="translate(200 400)">Station3</text>
        </g>
    </g>
</svg>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-10 14:43:00

tabindex添加到台站:

代码语言:javascript
复制
let stops = document.querySelector("#stops");
// all the g elements in the stops 
let gs = stops.querySelectorAll("g");

// for each g in the gs
gs.forEach((g, i) => {
  g.setAttribute('tabindex', i);
  // the rectangle in this g element
  let thisRect = g.querySelector("rect");
  // the circle in this g element
  let thisCircle = g.querySelector("circle");
  // the coords of the circle's center used for the transform-origin
  let x = thisCircle.getAttribute("cx");
  let y = thisCircle.getAttribute("cy");
  // the bounding box of the group
  let bb = g.getBBox();
  // set the rect's attributes
  thisRect.setAttributeNS(null, "x", bb.x);
  thisRect.setAttributeNS(null, "y", bb.y);
  thisRect.setAttributeNS(null, "width", bb.width);
  thisRect.setAttributeNS(null, "height", bb.height);
  
  // set the value for the transform-origin for this group
  g.style.transformOrigin =  `${x}px ${y}px`;
  

  
})

  document.getElementById('g3670').focus()
  
  function showmessage() {
  alert("heloo");
  }
代码语言:javascript
复制
text{
  font-family: Lato;
  font-size:16px;
}
g * {pointer-events:none;}
g rect{pointer-events:all;}
#stops g{transform: scale(1);cursor: pointer;transition:.3s}
#stops g:hover {
    transform: scale(2); 
}
#stops g:focus {
    outline: 0;
    transform: scale(2)
}
代码语言:javascript
复制
<svg id="Calque_1" data-name="Calque 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 775.43 469.98">
    <defs>
        <style>.cls-1{fill:none;stroke-width:5px;}.cls-1,.cls-2{stroke:#a15256;}.cls-2{fill:#fff;}.cls-3{isolation:isolate;font-size:42.79px;font-family:ArialMT, Arial;}</style>
    </defs>
    <title>line</title>
    <path id="path7" class="cls-1" d="M202,67.72,329.33,215.86" transform="translate(-200.1 -66.09)" />
    <path id="path8" class="cls-1" d="M329.35,215.87,449,355" transform="translate(-200.1 -66.09)" />
    <path id="path9" class="cls-1" d="M449,355c41.53,51.11,96.22,63.08,117.9,69.28" transform="translate(-200.1 -66.09)" />
    <path id="path10" class="cls-1" d="M566.86,424.29C655.43,460.48,977.38,391.48,973,536" transform="translate(-200.1 -66.09)" />
    <g id="stops">
        <g id="g3670">
            <rect fill="none"/>
            <circle class="cls-2" cx="129.24" cy="149.78" r="13.58" />
            <text id="text3668" class="cls-3" transform="translate(145 160)">Station1</text>
        </g>
        <g id="g3700">
            <rect fill="none"/>
            <circle class="cls-2" cx="248.91" cy="288.93" r="13.58" />
            <text id="text3698" class="cls-3" transform="translate(270 300)">Station2</text>
        </g>
        <g id="g3750">
            <rect fill="none"/>
            <circle class="cls-2" cx="366.75" cy="358.2" r="13.58" />
            <text id="text3748" class="cls-3" transform="translate(200 400)">Station3</text>
        </g>
    </g>
</svg>

票数 3
EN

Stack Overflow用户

发布于 2019-07-10 14:43:18

我就是这样做的:在单击时,我将为单击的组切换一个类active。在您的代码中,我添加了以下内容:

代码语言:javascript
复制
  g.addEventListener("click",()=>{
    g.classList.toggle("active")
  })

forEach里面。

代码语言:javascript
复制
let stops = document.querySelector("#stops");
// all the g elements in the stops 
let gs = stops.querySelectorAll("g");

// for each g in the gs
gs.forEach(g=>{
  // the rectangle in this g element
  let thisRect = g.querySelector("rect");
  // the circle in this g element
  let thisCircle = g.querySelector("circle");
  // the coords of the circle's center used for the transform-origin
  let x = thisCircle.getAttribute("cx");
  let y = thisCircle.getAttribute("cy");
  // the bounding box of the group
  let bb = g.getBBox();
  // set the rect's attributes
  thisRect.setAttributeNS(null, "x", bb.x);
  thisRect.setAttributeNS(null, "y", bb.y);
  thisRect.setAttributeNS(null, "width", bb.width);
  thisRect.setAttributeNS(null, "height", bb.height);
  
  // set the value for the transform-origin for this group
  g.style.transformOrigin =  `${x}px ${y}px`;
  
  
  g.addEventListener("click",()=>{
    g.classList.toggle("active")
  })
  

  
})

  document.getElementById('g3670').focus()
  
  function showmessage() {
  console.log("heloo");
  }
代码语言:javascript
复制
text{
  font-family: Lato;
  font-size:16px;
}
g * {pointer-events:none;}
g rect{pointer-events:all;}
#stops g{transform: scale(1);cursor: pointer;}
#stops g:hover {
    transform: scale(2); 
}
#stops g.active {
    transform: scale(2)
}
代码语言:javascript
复制
<svg id="Calque_1" data-name="Calque 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 775.43 469.98">
    <defs>
        <style>.cls-1{fill:none;stroke-width:5px;}.cls-1,.cls-2{stroke:#a15256;}.cls-2{fill:#fff;}.cls-3{isolation:isolate;font-size:42.79px;font-family:ArialMT, Arial;}</style>
    </defs>
    <title>line</title>
    <path id="path7" class="cls-1" d="M202,67.72,329.33,215.86" transform="translate(-200.1 -66.09)" />
    <path id="path8" class="cls-1" d="M329.35,215.87,449,355" transform="translate(-200.1 -66.09)" />
    <path id="path9" class="cls-1" d="M449,355c41.53,51.11,96.22,63.08,117.9,69.28" transform="translate(-200.1 -66.09)" />
    <path id="path10" class="cls-1" d="M566.86,424.29C655.43,460.48,977.38,391.48,973,536" transform="translate(-200.1 -66.09)" />
    <g id="stops">
        <g id="g3670">
            <rect fill="none"/>
            <circle class="cls-2" cx="129.24" cy="149.78" r="13.58" />
            <text id="text3668" class="cls-3" transform="translate(145 160)">Station1</text>
        </g>
        <g id="g3700">
            <rect fill="none"/>
            <circle class="cls-2" cx="248.91" cy="288.93" r="13.58" />
            <text id="text3698" class="cls-3" transform="translate(270 300)">Station2</text>
        </g>
        <g id="g3750">
            <rect fill="none"/>
            <circle class="cls-2" cx="366.75" cy="358.2" r="13.58" />
            <text id="text3748" class="cls-3" transform="translate(200 400)">Station3</text>
        </g>
    </g>
</svg>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56972410

复制
相关文章

相似问题

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