該賞金過期7天。這個問題的答案有資格獲得 50聲望獎勵。 艾丹·楊希望引起更多人對這個問題的關注。
我已經使用 A-frame ( https://aframe.io )創建了一個場景,目前我的場景中有一些雨和一個按鈕。我想做的是當按鈕被點擊時,一個函式發生,我想知道如何讓這個函式rain從場景中洗掉組件。代碼:
<head>
<script src="https://cdn.rawgit.com/aframevr/aframe/v0.4.0/dist/aframe-master.min.js"></script>
<script src="https://rawgit.com/takahirox/aframe-rain/master/build/aframe-rain.min.js"></script>
</head>
<body>
<button onclick="toggleRain()">
Toggle rain
</button>
<a-scene rain>
<a-entity position="0 0 10">
<a-camera></a-camera>
</a-entity>
<a-entity geometry="primitive:sphere"></a-entity>
<a-sky color="#222"></a-sky>
<a-entity light="type:directional;color:#666" position="-10 -10 -10"></a-entity>
</a-scene>
</body>
我想要發生的是當toggleRain()函式被觸發時,
<a-scene rain>代碼的一部分會改變并移除下雨的部分,所以只有
<a-scene>
當單擊按鈕時,這應該可以阻止雨水落下。只是為了澄清,當函式被觸發時,它應該rain從<a-scene rain>離開剛剛的屬性
<a-scene>
如何才能做到這一點?鏈接到包含代碼的小提琴:https : //jsfiddle.net/AidanYoung/z7qstgua/2/
uj5u.com熱心網友回復:
你可以removeAttribute()用來洗掉rain
function toggleRain() {
let a = document.getElementById('a');
if (a.hasAttribute('rain')) {
a.removeAttribute('rain');
} else {
a.setAttribute('rain', '1');
}
console.log ("ok");
}
<head>
<script src="https://cdn.rawgit.com/aframevr/aframe/v0.4.0/dist/aframe-master.min.js"></script>
<script src="https://rawgit.com/takahirox/aframe-rain/master/build/aframe-rain.min.js"></script>
</head>
<body>
<button onclick="toggleRain()">
Toggle rain
</button>
<a-scene id="a" rain>
<a-entity position="0 0 10">
<a-camera></a-camera>
</a-entity>
<a-entity geometry="primitive:sphere"></a-entity>
<a-sky color="#222"></a-sky>
<a-entity light="type:directional;color:#666" position="-10 -10 -10"></a-entity>
</a-scene>
</body>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/330890.html
標籤:javascript 功能 三.js 一个框架 虚拟机
