第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 setInterval 函數(shù)中訪問 SVG 數(shù)據(jù)屬性

如何在 setInterval 函數(shù)中訪問 SVG 數(shù)據(jù)屬性

SMILET 2023-06-09 17:38:48
我有一個帶有屬性的圓圈 svg?data-percent。(有開始和停止倒計時的按鈕)我想在函數(shù)data-percent內(nèi)部更改此屬性。setInterval我的目的是改data-percentwhile秒倒計時之類的(second * 10)。我可以在 SetInterval 之外手動更改值;var?second?=?8; var?round?=?document.querySelector('.round'); round.setAttribute('data-percent',?second?*?8);但是在 setInterval 函數(shù)內(nèi)部沒有任何變化,控制臺也沒有錯誤。$(document).ready(function() {? var $round = $('.round'),? ? ? roundRadius = $round.find('circle').attr('r'),? ? ? roundPercent = $round.data('percent'),? ? ? roundCircum = 2 * roundRadius * Math.PI,? ? ? roundDraw = roundPercent * roundCircum / 100? $round.css('stroke-dasharray', roundDraw? + ' 999')})var second = 10;//var round = document.querySelector('.round');//round.setAttribute('data-percent', second * 10);let play = true;function togglePlay(){? ? if(!play){? ? ? ? playCount();? ? ? ? document.querySelector('button').innerHTML='Pause';? ? } else {? ? ? ? pauseCount();? ? ? ? document.querySelector('button').innerHTML='Play';? ? }? ? play = !play;}let myTimer;function playCount() {? ? myTimer = setInterval(myCounter,1000);}function pauseCount() {? ? clearInterval(myTimer);}function myCounter (){? ? if(second == 0) {? ? ?clearInterval(myTimer);? ? }? ? var round = document.querySelector('.round');? ? round.setAttribute('data-percent', second * 10);? ? console.log('Hello World', second)? ? second--;}body {? margin: 0;}.point {? position: absolute;? top: 50%;? left: 50%;? transform: translate(-50%,-50%);}.round {? transform: rotate(-90deg);? transition: all 1s ease-in-out;??? /* SVG */? fill: none;? stroke: blue;? stroke-width: 8;? stroke-linecap: round;? stroke-dasharray: 0 999;}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>? <div class="point">? ? <svg class="round" viewbox="0 0 100 100" width="200" height="200" data-percent="">? ? ? <circle cx="50" cy="50" r="40" />??? ? </svg></div><button onclick="togglePlay()">Play</button>
查看完整描述

1 回答

?
qq_遁去的一_1

TA貢獻1725條經(jīng)驗 獲得超8個贊

我自己找到了解決問題的方法。我也必須更新 CSS stroke-dasharray。這是解決方案:


    

  var roundPercent = 10;

//var round = document.querySelector('.round');

//round.setAttribute('data-percent', second * 10);

let play = true;


function togglePlay(){

    if(!play){

        playCount();

        document.querySelector('button').innerHTML='Pause';

    } else {

        pauseCount();

        document.querySelector('button').innerHTML='Play';


    }

    play = !play;

}




let myTimer;

function playCount() {

    myTimer = setInterval(myCounter,1000);

  

}

function pauseCount() {

    clearInterval(myTimer);

}


function myCounter (){

    if(roundPercent == 0) {

     clearInterval(myTimer);

    }

  var $round = $('.round'),

      roundRadius = $round.find('circle').attr('r'),

      roundCircum = 2 * roundRadius * Math.PI,

      roundCalc = roundPercent *10,

      roundDraw = roundCalc * roundCircum / 100

      $round.css('stroke-dasharray', roundDraw  + ' 999')

      var round = document.querySelector('.round');

      round.setAttribute('data-percent', roundCalc);   

      console.log('Hello World', roundCalc)

      roundPercent--;

}

body {

  margin: 0;

}

.point {

  position: absolute;

  top: 50%;

  left: 50%;

  transform: translate(-50%,-50%);

}

.round {

  transform: rotate(-90deg);

  transition: all 1s ease-in-out;

  

  /* SVG */

  fill: none;

  stroke: blue;

  stroke-width: 8;

  stroke-linecap: round;

  stroke-dasharray: 0 999;

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<div class="point">

 <svg class="round" viewbox="0 0 100 100" width="200" height="200" data-percent="100" style="stroke-dasharray: 251.327, 999;">

      <circle cx="50" cy="50" r="40" />  

    </svg>

</div>


<button onclick="togglePlay()">Play</button>


查看完整回答
反對 回復(fù) 2023-06-09
  • 1 回答
  • 0 關(guān)注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號