前端初学者:如何调整文字动画CSS适配自建订阅表单(改字号无效,HTML存疑)
核心问题根源
- 动画的尺寸参数(字号、行高、容器高度、位移动画值)全部为全屏展示设计,仅修改字号不同步调整关联属性会直接导致布局错乱
- 打字机效果的遮罩层颜色沿用了原演示的深色背景色,与当前
#efefef的表单背景冲突 - form标签的
onsubmit属性外误加了反引号,会导致HTML解析错误、弹窗逻辑失效 - 动画容器未做居中适配,与表单默认的文本居中逻辑不兼容
- 表单设置了固定200px高度,容易导致内容溢出被裁切
适配修改步骤
- 同步调整所有动画关联尺寸:将原60px字号统一改为适配表单的24px,对应行高、容器高度同步改为36px,滑动动画的总位移值改为
-144px(4行*36px/行) - 将打字机遮罩层的背景色改为表单背景色
#efefef,边框颜色保留原红色 - 移除
onsubmit属性外的多余反引号 - 给动画容器
wrapper增加justify-content: center属性,实现表单内居中 - 给动态文本容器
dynamic-txts增加最大宽度限制,避免长文本溢出表单边界 - 将表单的固定高度改为自适应,避免内容排布溢出被裁切
修改后完整代码
<!DOCTYPE html> <html> <head> <style> /* 找到的文字动画原始CSS */ @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap'); *{ margin: 0; padding: 0; box-sizing: border-box; font-family: 'Poppins', sans-serif; } body{ display: flex; align-items: center; justify-content: center; min-height: 100vh; background: #343F4F; } .wrapper{ display: flex; justify-content: center; } .wrapper .static-txt{ color: #000; font-size: 24px; font-weight: 400; line-height: 36px; } .wrapper .dynamic-txts{ margin-left: 15px; height: 36px; line-height: 36px; max-width: calc(100% - 100px); overflow: hidden; } .dynamic-txts li{ list-style: none; color: #FC6D6D; font-size: 24px; font-weight: 500; position: relative; top: 0; animation: slide 12s steps(4) infinite; } @keyframes slide { 100%{ top: -144px; } } .dynamic-txts li span{ position: relative; margin: 5px 0; line-height: 36px; } .dynamic-txts li span::after{ content: ""; position: absolute; left: 0; height: 100%; width: 100%; background: #efefef; border-left: 2px solid #FC6D6D; animation: typing 3s steps(10) infinite; } @keyframes typing { 40%, 60%{ left: calc(100% + 30px); } 100%{ left: 0; } } /* 我自建表单的原始CSS */ form { border: 5px solid #000; padding: 10px; text-align: center; border-left-width: 10px; border-right-width: 10px; width: 500px; background-color: #efefef; height: auto; } input[type=submit] { display: inline; padding: 10px 30px; font-weight: 300; border-radius: 200px; transition: background-color 0.2s, border 0.2s, color 0.2s; font-size: 15px; } input[type=submit] { background-color: #ff0000; color: #fff; border: 1px solid #000; font-weight: bold; } input[type=submit]:hover, input[type=submit]:active { background-color: #ff5e5e; } .directions { font-weight: bold; } input[type=text] { width: 200px; } </style> </head> <body> <form action="https://feedburner.google.com/fb/a/mailverify" method="post" target="popupwindow" onsubmit="window.open('https://feedburner.google.com/fb/a/mailverify?uri=blogspot/BKkas', 'popupwindow', 'scrollbars=yes,width=550,height=520');return true"> <div class="wrapper"> <div class="static-txt">Subscribe</div> <ul class="dynamic-txts"> <li><span>Enter Your Email</span></li> <li><span>To get the most recent news</span></li> <li><span>delivered right to your inbox.</span></li> <li><span>SIGN-UP NOW!</span></li> </ul> </div> <p><input type="text" name="email"/></p><br/> <input type="hidden" value="blogspot/BKkas" name="uri"/> <input type="hidden" name="loc" value="en_US"/> <input type="submit" value="Subscribe" /> </form> </body> </html>
内容的提问来源于stack exchange,提问作者Mike
相关产品推荐
相关产品推荐

