css3的transform屬性
問題描述
當你對一個元素進行了translateY(10px)操作,再對它進行rotateZ(45deg)操作,此時該元素的旋轉(zhuǎn)中心卻是以translateY之前的狀態(tài)作為旋轉(zhuǎn)中心,這是為什么?
<!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)'; } })
相關(guān)文章:
1. php - 微信開發(fā)驗證服務(wù)器有效性2. javascript - 我的站點貌似被別人克隆了, google 搜索特定文章,除了域名不一樣,其他的都一樣,如何解決?3. [python2]local variable referenced before assignment問題4. javascript - 求幫助 , ATOM不顯示界面!!!!5. Python2中code.co_kwonlyargcount的等效寫法6. python中怎么對列表以區(qū)間進行統(tǒng)計?7. 求救一下,用新版的phpstudy,數(shù)據(jù)庫過段時間會消失是什么情況?8. javascript - vue+iview upload傳參失敗 跨域問題后臺已經(jīng)解決 仍然報403,這是怎么回事啊?9. html - 移動端radio無法選中10. mysql - 請問數(shù)據(jù)庫字段為年月日,傳進的參數(shù)為月,怎么查詢那個月所對應(yīng)的數(shù)據(jù)
