css3的transform屬性
問題描述
當你對一個元素進行了translateY(10px)操作,再對它進行rotateZ(45deg)操作,此時該元素的旋轉中心卻是以translateY之前的狀態作為旋轉中心,這是為什么?
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title></title> <style media="screen"> * {margin: 0; padding: 0;} .b { width: 50px; height: 50px; /*border-radius: 50%;*/ background: #000; position: relative; }; ul { width: 20px; height: 20px; /*background: #fff;*/ position: absolute; top: 50%; left: 50%; margin-top: -10px; margin-left: -10px; } li { width: 20px; height: 2px; background: #fff; position: absolute; top: 50%; margin-top: (-1px); transform: translateY(3.75px); transition: all 1s; } li:nth-child(2) { transform: translateY(-3.75px); } </style> </head> <body> <div class="b"> <ul> <li></li> <li></li> </ul> </div> </body> <script type="text/javascript"> var b = document.querySelector('.b'); var lis = document.querySelectorAll('li'); var bol = false; b.addEventListener('click',function(){ bol = !bol; if(bol) { lis[0].style.transform = 'rotateZ(45deg)'; lis[1].style.transform = 'rotateZ(-45deg)'; } else { lis[0].style.transform = 'rotateZ(0deg)'; lis[1].style.transform = 'rotateZ(0deg)'; } }) </script></html>
問題解答
回答1:都寫在一個transform里
var b = document.querySelector('.b'); var lis = document.querySelectorAll('li'); var bol = false; b.addEventListener('click',function(){ bol = !bol; if(bol) { lis[0].style.transform = 'rotateZ(45deg)'; lis[1].style.transform = 'rotateZ(-45deg)'; } else { lis[0].style.transform = 'translateY(3.75px) rotateZ(0deg)'; lis[1].style.transform = 'translateY(-3.75px) rotateZ(0deg)'; } })
相關文章:
1. css3 - 這個形狀使用CSS怎么寫出來?2. 這種數據怎么合并???3. 請教各位大佬,瀏覽器點 提交實例為什么沒有反應4. javascript - 如何使用loadash對[object,object,object]形式的數組進行比較5. mysql優化 - 關于mysql分區6. 如何分別在Windows下用Winform項模板+C#,在MacOSX下用Cocos Application項目模板+Objective-C實現一個制作游戲的空的黑窗口?7. angular.js - 百度支持_escaped_fragment_嗎?8. javascript - ionic2 input autofocus 電腦成功,iOS手機鍵盤不彈出9. nlp - python如何對一篇文章自動分段?10. nginx - 關于vue項目部署到ngnix后出現的問題
