冠县网站建设价格,邵阳seo,WordPress 怎么添加关键字代码,企业邮箱注册申请一般多少钱jQuery UI SwitchClass#xff08;转换 Class#xff09;特效实例
switchClass() 是 jQuery UI Effects 中非常强大的方法#xff0c;它可以以动画效果从一个 CSS 类平滑转换到另一个 CSS 类。本质上是先移除旧类#xff0c;再添加新类#xff0c;整个过程平滑过渡#…jQuery UI SwitchClass转换 Class特效实例switchClass()是 jQuery UI Effects 中非常强大的方法它可以以动画效果从一个 CSS 类平滑转换到另一个 CSS 类。本质上是先移除旧类再添加新类整个过程平滑过渡常用于状态切换、主题更换、模式切换正常/错误/成功、动画按钮等场景。与addClass()、removeClass()、toggleClass()不同的是switchClass() 明确指定“从 A 切换到 B”更适合有明确前后状态的交互。官方文档https://jqueryui.com/switchClass/下面提供几个渐进实例代码使用最新 CDN可直接复制到 HTML 文件测试。1.基础转换 Class 动画从一种状态平滑切换到另一种状态。!DOCTYPEhtmlhtmlheadmetacharsetutf-8titlejQuery UI SwitchClass 示例/titlelinkrelstylesheethref//code.jquery.com/ui/1.13.2/themes/smoothness/jquery-ui.cssscriptsrc//code.jquery.com/jquery-3.6.0.min.js/scriptscriptsrc//code.jquery.com/ui/1.13.2/jquery-ui.min.js/scriptstyle.normal{background:#607D8B;color:white;width:200px;height:120px;padding:20px;text-align:center;line-height:120px;border-radius:8px;}.success{background:#4CAF50;color:white;font-size:1.6em;transform:scale(1.1);box-shadow:0 10px 30pxrgba(76,175,80,0.5);border-radius:15px;}.warning{background:#FF9800;color:white;transform:rotate(3deg);box-shadow:0 0 20pxrgba(255,152,0,0.6);}.error{background:#f44336;color:white;transform:scale(0.95)rotate(-2deg);box-shadow:0 0 25pxrgba(244,67,54,0.7);}#box{margin:40px auto;cursor:pointer;}/style/headbodybuttonidtoSuccess切换到成功状态/buttonbuttonidtoWarning切换到警告状态/buttonbuttonidtoError切换到错误状态/buttonbuttonidtoNormal恢复正常/buttondividboxclassnormal当前状态正常/divscript$(function(){$(#toSuccess).click(function(){$(#box).switchClass(normal,success,1200);});$(#toWarning).click(function(){$(#box).switchClass(normal,warning,1000);});$(#toError).click(function(){$(#box).switchClass(normal,error,1000);});$(#toNormal).click(function(){// 从当前任意状态切换回 normal$(#box).switchClass(success warning error,normal,1500);});});/script/body/html2.带缓动 回调的转换使用弹跳缓动并在转换完成后提示。buttonidmodeSwitch切换模式/buttonstyle.light{background:#FFFDE7;color:#333;}.dark{background:#212121;color:#FFFDE7;font-weight:bold;}/stylescript$(#modeSwitch).click(function(){$(#box).switchClass(light,dark,1500,easeInOutBack,function(){alert(已切换到暗黑模式);}).switchClass(dark,light,1500,easeInOutBack,function(){alert(已切换回明亮模式);});// 注意这里会排队执行形成来回切换});/script3.多状态循环切换实现三种状态循环正常 → 警告 → 错误 → 正常。buttonidcycle循环切换状态/buttonscriptvarstates[normal,warning,error];varcurrent0;$(#cycle).click(function(){varnextstates[(current1)%states.length];$(#box).switchClass(states[current],next,1000);current(current1)%states.length;});/script4.常见应用表单验证状态切换输入框根据验证结果切换样式。inputtypetextidusernameplaceholder请输入用户名buttonidvalidate验证/buttonstyle.valid{border:3px solid #4CAF50;background:#e8f5e8;}.invalid{border:3px solid #f44336;background:#ffebee;}.default{border:1px solid #ccc;}/stylescript$(#validate).click(function(){varval$(#username).val();if(val.length3){$(#username).switchClass(invalid default,valid,800);}else{$(#username).switchClass(valid default,invalid,800);}});/script小技巧移除旧类、添加新类是原子操作中间不会出现无样式状态。支持多个旧类switchClass(old1 old2, new, duration)队列动画连续调用 switchClass 会自动排队形成连续状态转换。与 toggleClass 区别toggleClass 是“有/无”切换switchClass 是“从A到B”明确转换。switchClass() 是实现明确状态迁移的最佳选择常用于主题切换、模式切换日间/夜间、步骤向导状态、按钮多态反馈等。如果你需要暗黑模式全局切换、步骤进度条状态动画或结合 Dialog 的状态反馈示例请告诉我