亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如果屏幕尺寸改變,如何改變圓圈的大?。?/h1>

如果屏幕尺寸改變,如何改變圓圈的大?。?/h1>
慕妹3146593 2023-10-30 20:13:58
我在互聯網上看到了一段代碼,我試圖根據屏幕尺寸改變圓圈的大小,我嘗試將整個JS代碼放在if語句和我在網站上看到的一個函數中,它有效,但需要縮放后刷新頁面,那么是否可以隨著屏幕尺寸的變化而改變圓圈的大?。课覐囊韵挛恢毛@取了代碼:https ://codepen.io/Yago/pen/WNbxjYw/** * index.js * - All our useful JS goes here, awesome! Maruf-Al Bashir Reza */function myFunction(x) {  if (x.matches) { // If media query matches          $(document).ready(function($) {  function animateElements() {    $('.progressbar').each(function() {      var elementPos = $(this).offset().top;      var topOfWindow = $(window).scrollTop();      var percent = $(this).find('.circle').attr('data-percent');      var percentage = parseInt(percent, 10) / parseInt(100, 10);      var animate = $(this).data('animate');      if (elementPos < topOfWindow + $(window).height() - 30 && !animate) {        $(this).data('animate', true);        $(this).find('.circle').circleProgress({          startAngle: -Math.PI / 2,          value: percent / 100,          thickness: 4,          lincCape:'round',          emptyFill:'#d4d4d4',          fill: {            color: '#1F88E9'          },                      size:80        })      }    });  }  // Show animated elements  animateElements();  $(window).scroll(animateElements);});  } else {          $(document).ready(function($) {    function animateElements() {    $('.progressbar').each(function() {      var elementPos = $(this).offset().top;      var topOfWindow = $(window).scrollTop();      var percent = $(this).find('.circle').attr('data-percent');      var percentage = parseInt(percent, 10) / parseInt(100, 10);      var animate = $(this).data('animate');      if (elementPos < topOfWindow + $(window).height() - 30 && !animate) {        $(this).data('animate', true);        $(this).find('.circle').circleProgress({          startAngle: -Math.PI / 2,          value: percent / 100,          thickness: 4,          lincCape:'round',          emptyFill:'#d4d4d4',          fill: {            color: '#1F88E9'          },                      size:150        })      }    });  }
查看完整描述

1 回答

?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

你的問題是你必須添加調整大小事件監聽器,并且true在動畫完成時使動畫無效,以便條件可以再次運行以調整圓圈大小,你會注意到,每當您調整頁面大小時,它都會重新動畫。


/**

?* index.js

?* - All our useful JS goes here, awesome!

?Maruf-Al Bashir Reza

?*/


function myFunction(x) {

??

? let size = 150;

? if (x.matches) {

? ? // If media query matches

? ? size = 80;

? }


? ? function animateElements() {

? ? ? $(".progressbar").each(function () {

? ? ? ? var elementPos = $(this).offset().top;

? ? ? ? var topOfWindow = $(window).scrollTop();

? ? ? ? var percent = $(this).find(".circle").attr("data-percent");

? ? ? ? var percentage = parseInt(percent, 10) / parseInt(100, 10);

? ? ? ? var animate = $(this).data("animate");

? ? ? ? if (elementPos < topOfWindow + $(window).height() - 30 && !animate) {

? ? ? ? ? $(this).data("animate", true);

? ? ? ? ? $(this)

? ? ? ? ? ? .find(".circle")

? ? ? ? ? ? .circleProgress({

? ? ? ? ? ? ? startAngle: -Math.PI / 2,

? ? ? ? ? ? ? value: percent / 100,

? ? ? ? ? ? ? thickness: 4,

? ? ? ? ? ? ? lincCape: "round",

? ? ? ? ? ? ? emptyFill: "#d4d4d4",

? ? ? ? ? ? ? fill: {

? ? ? ? ? ? ? ? color: "#1F88E9",

? ? ? ? ? ? ? },


? ? ? ? ? ? ? size: size,

? ? ? ? ? ? });

? ? ? ? ? $(this).data("animate", false);

? ? ? ? }

? ? ? });

? ? }


? ? // Show animated elements

? ? animateElements();

? ? $(window).scroll(animateElements);

}


var x = window.matchMedia("(max-width: 700px)");

myFunction(x);


$(window).on("resize", function (e) {

? var x = window.matchMedia("(max-width: 700px)");

? myFunction(x); // Call listener function at run time

});

/**

?* index.scss

?* - Add any styles you want here!

?*/


body {

? background: #f5f5f5;

}


.progressbar {

? display: inline-block;

? width: 100px;

? margin: 25px;

}


.circle {

? width: 100%;

? margin: 0 auto;

? margin-top: 10px;

? display: inline-block;

? position: relative;

? text-align: center;

}


.circle canvas {

? vertical-align: middle;

}


.circle div {

? position: absolute;

? top: 30px;

? left: 0;

? width: 100%;

? text-align: center;

? line-height: 40px;

? font-size: 20px;

}


.circle strong i {

? font-style: normal;

? font-size: 0.6em;

? font-weight: normal;

}


.circle span {

? display: block;

? color: #aaa;

? margin-top: 12px;

}

<!DOCTYPE html>

<html>


<head>


? <!--? Meta? -->

? <meta charset="UTF-8" />

? <title>My New Pen!</title>


? <!--? Styles? -->

? <link rel="stylesheet" href="styles/index.processed.css">

</head>


<body>

? <h1 style="margin:auto;text-align:center;color:skyblue;">Circle Progressbar When Scroll</h1>

? <div style="width:100%;height:800px;">↓ Scroll down ↓</div>


? <h3>Title (Placeholder)</h3>


? <div class="progressbar" data-animate="false">

? ? <div class="circle" data-percent="100">

? ? ? <div></div>

? ? ? <p>Testing</p>

? ? </div>

? </div>

? <div class="progressbar" data-animate="false">

? ? <div class="circle" data-percent="30.5">

? ? ? <div></div>

? ? ? <p>Testing</p>

? ? </div>

? </div>

? <div class="progressbar" data-animate="false">

? ? <div class="circle" data-percent="77">

? ? ? <div></div>

? ? ? <p>Testing</p>

? ? </div>

? </div>

? <div class="progressbar" data-animate="false">

? ? <div class="circle" data-percent="49">

? ? ? <div></div>

? ? ? <p>Testing</p>

? ? </div>

? </div>

? <div style="width:100%;height:500px;"></div>

? <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

? <script src="https://rawgit.com/kottenator/jquery-circle-progress/1.2.1/dist/circle-progress.js"></script>

? <script src="scripts/index.js"></script>

</body>


</html>


查看完整回答
反對 回復 2023-10-30
  • 1 回答
  • 0 關注
  • 150 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號