我一直在嘗試用一個(gè)按鈕旋轉(zhuǎn)一個(gè)帶有項(xiàng)目的圓,當(dāng)我直接在css中改變圓的旋轉(zhuǎn)值時(shí),它工作正常,但在html代碼中似乎有一個(gè)問題。 html代碼:
<html>
<head>
<title>web desgin test</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="main">
<nav>
<div class="logo">
<img
src="C:/Users/USER/Downloads/RealPhone_WebPage_Images/images/logo.png"
alt="">
</div>
<div class="links">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Phone</a></li>
<li><a href="#">Accessories</a></li>
<li><a href="#">Cart</a></li>
</ul>
</div>
</nav>
<div class="information">
<img
src="C:/Users/USER/Downloads/RealPhone_WebPage_Images/images/mobile.png"
class="mobile">
<div id="circle">
<div class="feature one">
<img
src="C:/Users/USER/Downloads/RealPhone_WebPage_Images/images/camera.png"
alt="">
<div>
<h1>Camera</h1>
<p>108MP Main camera</p>
</div>
</div>
<div class="feature two">
<img src="C:/Users/USER/Downloads/RealPhone_WebPage_Images/images/processor.png" alt="">
<div>
<h1>processor</h1>
<p>Exynos 990</p>
</div>
</div>
<div class="feature three">
<img src="C:/Users/USER/Downloads/RealPhone_WebPage_Images/images/display.png" alt="">
<div>
<h1>Display</h1>
<p>6.9 inch, 1440px</p>
</div>
</div>
<div class="feature four">
<img src="C:/Users/USER/Downloads/RealPhone_WebPage_Images/images/battery.png" alt="">
<div>
<h1>Battery</h1>
<p>4500 mA</p>
</div>
</div>
</div>
</div>
<div class="controls">
<img src="C:/Users/USER/Downloads/RealPhone_WebPage_Images/images/arrow.png" id="upBtn">
<h3>Features</h3>
<img src="C:/Users/USER/Downloads/RealPhone_WebPage_Images/images/arrow.png" id="downBtn">
</div>
</div>
<script>
var circle = document.getElementById('circle');
var upBtn = document.getElementById('upBtn');
var downBtn = document.getElementById('downBtn');
var rotateValue = circle.style.transform();
var rotateSum;
upBtn.onclick = function() {
rotateSum = rotateValue + "rotate(-90deg)";
circle.style.transform = rotateSum;
rotateValue = rotateSum;
}
</script>
css代碼:
#circle {
width: 1000px;
height: 1000px;
position: absolute;
top: 0;
left: 0;
border-radius: 50%;
transform: rotate(0deg);
.controls {
position: absolute;
right: 10%;
top: 50%;`enter code here`
transform: translateY(-50%);
text-align: center;
}
.controls h3 {
margin: 15px 0;
color: #fff;
}
#upBtn {
width: 15px;
cursor: pointer;
}
#downBtn {
width: 15px;
cursor: pointer;
transform: rotate(180deg);
}
................................................................................
# # #例如,如果id是#circle,則變量聲明是錯(cuò)誤的
document . getelementbyid(circle);-& gt;document . getelementbyid(' circle ');
當(dāng)你根據(jù)這個(gè)函數(shù)發(fā)布所有的html時(shí)會(huì)更好
# # #您的html代碼中沒有圓圈... 所以當(dāng)你這樣做的時(shí)候:
var circle = document.getElementById(circle);
變量circle是空的,因?yàn)闆]有一個(gè)html元素的id是circle,而你正試圖獲取一個(gè)這樣的元素 此外,當(dāng)您這樣做時(shí):
document . getelementbyid(circle); 將在每個(gè)id中查找一個(gè)字符串,因此它應(yīng)該是:
document.getElementById('circle');
編輯: 為了清楚起見,當(dāng)您使用選擇器時(shí),您正在尋找一個(gè)匹配的字符串,當(dāng)您這樣做時(shí):
var circle = document.getElementById(circle);
var upBtn = document.getElementById(upBtn);
var downBtn = document.getElementById(downBtn);
應(yīng)該是這樣的:
var circle = document.getElementById('circle');
var upBtn = document.getElementById('upBtn');
var downBtn = document.getElementById('downBtn');
在你說的第一個(gè)中,尋找一個(gè)值為os circle的字符串
var circle = 'notCircle'
var circle = document.getElementById(circle);
console.log(circle)
在第二個(gè)例子中,你在html ids上直接尋找x字符串