简体   繁体   English

当我使用js更改className时,css转换不起作用

[英]css transition didn't work when i change the className using js

When i change the className with js, it should TRANSITION in my mind.. But.. the code below didn't work 当我用js更改className时,在我的脑海中应该转换。....以下代码不起作用

  let canvas = document.getElementById("canvas"); let types = 'center' canvas.addEventListener('click', function (e) { let target = this target.style.position = 'relative' target.style.overflow = 'hidden' let ripple = target.querySelector('.ripple') /* 无ripple 说明第一次点击 */ if (!ripple) { ripple = document.createElement('span') ripple.className = 'ripple' ripple.style.height = ripple.style.width = `120px` target.appendChild(ripple) } else { ripple.className = 'ripple' } //ripple.style.top = e.pageY + 'px'; // ripple.style.left = e.pageX + 'px' // console.log(e.pageY); //console.log(e.pageX); switch (types) { case 'center': ripple.style.top = `${ripple.offsetHeight / 2}px` ripple.style.left = `${ripple.offsetWidth / 2}px` break default: ripple.style.top = `${ripple.offsetHeight / 2}px` ripple.style.left = `${ripple.offsetWidth / 2}px` } ripple.style.backgroundColor = '#999' ripple.className = 'ripple active' }) 
 .ripple { position: absolute; border-radius: 100%; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; -webkit-transform: scale(0); -ms-transform: scale(0); transform: scale(0); } .ripple.active { -webkit-transition: opacity 1.2s ease-out, -webkit-transform 0.6s ease-out; transition: opacity 1.2s ease-out, -webkit-transform 0.6s ease-out; transition: opacity 1.2s ease-out, transform 0.6s ease-out; transition: opacity 1.2s ease-out, transform 0.6s ease-out, -webkit-transform 0.6s ease-out; -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2);} 
 <div id="canvas" style="width: 100%;height: 1024px"> canvas </div> 

As you can see the code above, if i change the className and using the ripple.style.top = e.pageY + 'px'; ripple.style.left = e.pageX + 'px' 如您在上面的代码中看到的,如果我更改className并使用ripple.style.top = e.pageY + 'px'; ripple.style.left = e.pageX + 'px' ripple.style.top = e.pageY + 'px'; ripple.style.left = e.pageX + 'px' It cant emit the transition..However if using the switch code, which is the same as the code above i think, it worked! ripple.style.top = e.pageY + 'px'; ripple.style.left = e.pageX + 'px'它不能发出过渡ripple.style.top = e.pageY + 'px'; ripple.style.left = e.pageX + 'px'但是,如果使用开关代码,它与上面我认为的代码相同,就可以了! It's so confusing, can anybody help me? 太混乱了,有人可以帮助我吗?

As I run your code with ripple.style.left = e.pageX + 'px' I saw that without use console.log() it does not work so as I understood you have to give the program few milliseconds to "rest" and set the new position to solve that I use setTimeout with 0 time then I set the new className 当我使用ripple.style.left = e.pageX + 'px'运行代码时,我发现如果不使用console.log()它将无法正常工作,因为据我了解,您必须给程序几毫秒的时间才能“休息”,设置新position以解决我将setTimeout0时间一起使用的问题,然后设置新的className

More information: 更多信息:

See working code: 查看工作代码:

  let canvas = document.getElementById("canvas"); let types = 'center' canvas.addEventListener('click', function (e) { let target = this target.style.position = 'relative' target.style.overflow = 'hidden' let ripple = target.querySelector('.ripple') /* 无ripple 说明第一次点击 */ if (!ripple) { ripple = document.createElement('span') ripple.className = 'ripple' ripple.style.height = ripple.style.width = `120px` target.appendChild(ripple) } else { ripple.className = 'ripple' } ripple.style.top = e.pageY + 'px'; ripple.style.left = e.pageX + 'px'; setTimeout(function(){ ripple.style.backgroundColor = '#999999'; ripple.className = 'ripple active' },0); }) 
 .ripple { position: absolute; border-radius: 100%; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; -webkit-transform: scale(0); -ms-transform: scale(0); transform: scale(0); } .ripple.active { -webkit-transition: opacity 1.2s ease-out, -webkit-transform 0.6s ease-out; transition: opacity 1.2s ease-out, -webkit-transform 0.6s ease-out; transition: opacity 1.2s ease-out, transform 0.6s ease-out; transition: opacity 1.2s ease-out, transform 0.6s ease-out, -webkit-transform 0.6s ease-out; -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); } 
 <div id="canvas" style="width: 100%;height: 1024px"> canvs </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我在 React 中动态设置类名时,CSS 转换不起作用 - CSS transition not working when I set a className dynamically in React 在ie中使用js时,className在某些代码中不起作用。 有谁知道为什么? - when using js in ie, className doesn't work in certain code. is anyone knows the why? JS Code无法正常工作,尝试在css课程中进行演示 - JS Code didn't work, trying do a demo in css lessons @Import 另一个 css 文件在我处理 Laravel 9 Inertia React 堆栈时不起作用 - @Import another css file didn't work when i'm working on Laravel 9 Inertia React stack 当我使用公共文件夹Symfony 4时,我的css和js文件没有加载 - My css and js files didn't load when i use the public folder Symfony 4 JS / CSS问题:我的元素没有完全重叠,当使用z-index时,它停止了javascript - JS/CSS Issue : my element didn't fully overlapped, when using z-index it stopped the javascript 无法使Vue.js CSS过渡正常工作? - Can't get Vue.js CSS transition to work? 转换不起作用(CSS 和 JS)切换 class - Transition won't work (CSS & JS) toggle class import _ from 'lodash-es/lodash.js' 曾经可以工作。 现在它没有,但我没有改变任何东西 - import _ from 'lodash-es/lodash.js' used to work. Now it doesn't but I didn't change anything 显示/隐藏div的JS源代码没有按我预期的那样工作 - JS source to show/hide divs, didn't work as I expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM