2016-08-13 71 views
-2

*,當我選擇yes或no後,寬度立即出現。我想要的是動畫寬度。我用動畫搜索了另一個例子,但我仍然無法在這個例子中應用動畫。如何在w3schools的這個輪詢示例中動畫顯示w3schools中的ajax poll的寬度

達到此目的的最佳方法是什麼? *

<!DOCTYPE HTML> 
 
<html> 
 
<head> 
 
<script> 
 
function getVote(int) { 
 
    if (window.XMLHttpRequest) { 
 
    // code for IE7+, Firefox, Chrome, Opera, Safari 
 
    xmlhttp=new XMLHttpRequest(); 
 
    } else { // code for IE6, IE5 
 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
 
    } 
 
    xmlhttp.onreadystatechange=function() { 
 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
 
     document.getElementById("poll").innerHTML=xmlhttp.responseText; 
 
    } 
 
    } 
 
    xmlhttp.open("GET","poll_vote.php?vote="+int,true); 
 
    xmlhttp.send(); 
 
} 
 
</script> 
 
</head> 
 
<body><div id="poll"> 
 
<h3>Do you like PHP and AJAX so far?</h3> 
 
<form> 
 
Yes: 
 
<input type="radio" name="vote" value="0" onclick="getVote(this.value)"> 
 
<br>No: 
 
<input type="radio" name="vote" value="1" onclick="getVote(this.value)"> 
 
</form> 
 
</div> 
 

 
</body> 
 
</html>

<?php 
 
$vote = $_REQUEST['vote']; 
 

 
//get content of textfile 
 
$filename = "poll_result.txt"; 
 
$content = file($filename); 
 

 
//put content in array 
 
$array = explode("||", $content[0]); 
 
$yes = $array[0]; 
 
$no = $array[1]; 
 

 
if ($vote == 0) { 
 
    $yes = $yes + 1; 
 
} 
 
if ($vote == 1) { 
 
    $no = $no + 1; 
 
} 
 
//insert votes to txt file 
 
$insertvote = $yes."||".$no; 
 
$fp = fopen($filename,"w"); 
 
fputs($fp,$insertvote); 
 
fclose($fp); 
 
?> 
 

 
<h2>Result:</h2> 
 
<table> 
 
<tr> 
 
<td>Yes:</td> 
 
<td> 
 
<div style="background-color: blue; width:<?php echo(100*round($yes/($no+$yes),2)); ?>px; height:20px "> 
 
<?php echo(100*round($yes/($no+$yes),2)); ?>% 
 
</div> 
 
</td> 
 
</tr> 
 
<tr> 
 
<td>No:</td> 
 
<td> 
 
<div style="background-color: blue; width:<?php echo(100*round($no/($no+$yes),2)); ?>px; height:20px "> 
 
<?php echo(100*round($no/($no+$yes),2)); ?>% 
 
</div> 
 
</td> 
 
</tr> 
 
</table>

+0

月1日:您遺漏了DIV '民意調查' 結束標記:''

第二 – Jeff

+0

:'int'是JS的保留字,因此重新命名'int'到像'value'或'投票' – Jeff

+0

第3次:你錯過了關閉';'在高度後的樣式定義。這不應該犯錯誤,但仍然更好地學習編碼! - 在<<?之後相同php回聲.....; ?> – Jeff

回答

0

我發現使用CSS動畫屬性和這裏的方法是在編輯PHP頁面

<?php 
 
$vote = $_GET['vote']; 
 

 
//get content of textfile 
 
$filename = "poll_result.txt"; 
 
$content = file($filename); 
 

 
//put content in array 
 
$array = explode("||", $content[0]); 
 
$yes = $array[0]; 
 
$no = $array[1]; 
 

 
if ($vote == 0) { 
 
    $yes = $yes + 1; 
 
} 
 
if ($vote == 1) { 
 
    $no = $no + 1; 
 
} 
 

 
//insert votes to txt file 
 
$insertvote = $yes."||".$no; 
 
$fp = fopen($filename,"w"); 
 
fputs($fp,$insertvote); 
 
fclose($fp); 
 
?> 
 
<!DOCTYPE HTML> 
 
<html> 
 
<head> 
 
<style type="text/css"> 
 
    .q1{ 
 
     width: <?php echo intval(100*round($yes/($no+$yes),2));?>px; 
 
     height: 20px; 
 
     background-color: red; 
 
     animation-name: first; 
 
     animation-duration: 1s; 
 
     animation-delay: 0s; 
 
     -webkit-animation-name: first; 
 
     -webkit-animation-duration: 1s; 
 
     -webkit-animation-delay: 0s; 
 
    } 
 

 
    @keyframes first { 
 
     0% {width: 0px;} 
 
     100% {width: <?php echo(100*round($yes/($no+$yes),2));?>;} 
 
    } 
 
    @-webkit-keyframes first { 
 
     0% {width: 0px;} 
 
     100% {width: <?php echo(100*round($yes/($no+$yes),2));?>;} 
 
    } 
 
    
 
    .q2{ 
 
     width: <?php echo intval(100*round($no/($no+$yes),2));?>px; 
 
     height: 20px; 
 
     background-color: red; 
 
     animation-name: second; 
 
     animation-duration: 1s; 
 
     animation-delay: 0s; 
 
     -webkit-animation-name: second; 
 
     -webkit-animation-duration: 1s; 
 
     -webkit-animation-delay: 0s; 
 
    } 
 

 
    @keyframes second { 
 
     0% {width: 0px;} 
 
     100% {width: <?php echo(100*round($no/($no+$yes),2));?>;} 
 
    } 
 
    @-webkit-keyframes second { 
 
     0% {width: 0px;} 
 
     100% {width: <?php echo(100*round($no/($no+$yes),2));?>;} 
 
    } 
 
</style> 
 
</head> 
 

 
<body> 
 
<h2>Result:</h2> 
 
<table> 
 
<tr> 
 
<td>Yes:</td> 
 
<td> 
 
<div class="q1"> 
 
<?php echo(100*round($yes/($no+$yes),2)); ?>% 
 
</div> 
 
</td> 
 
</tr> 
 
<tr> 
 
<td>No:</td> 
 
<td> 
 
<div class="q2"> 
 
<?php echo(100*round($no/($no+$yes),2)); ?>% 
 
</div> 
 
</td> 
 
</tr> 
 
</table> 
 
<body> 
 
</html>
代碼

相關問題