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. python 计算两个时间相差的分钟数,超过一天时计算不对2. spring-mvc - spring-session-redis HttpSessionListener失效3. node.js - express框架,设置浏览器从缓存中读取静态文件,只有js从缓存中读取了,css还有一些图片为何没有从缓存中读取?4. 做Redis集群的时候,可不可以将Master实例和Slave实例放在一个主机当中?5. python - flask post提交timestamp不能作为参数,这是为什么?6. dockerfile - 我用docker build的时候出现下边问题 麻烦帮我看一下7. android glide asbitmap 在baseadpter中的问题8. jquery - js向两边展开9. javascript - 如何获取未来元素的父元素在页面中所有相同元素中是第几个?10. 前端 - @media query 使用出现的问题?

网公网安备