我正在研究以下代碼:
* {
box-sizing: border-box;
}
body {
font-family: "Open Sans", sans-serif;
font-size: 16px;
-webkit-font-smoothing: antialiased;
text-rendering: optimizelegibility;
color: #223254;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.cbx {
-webkit-user-select: none;
user-select: none;
cursor: pointer;
padding: 6px 8px;
border-radius: 6px;
overflow: hidden;
transition: all 0.2s ease;
}
.cbx:not(:last-child) {
margin-right: 6px;
}
.cbx:hover {
background: rgba(0, 119, 255, 0.06);
}
.cbx span {
float: left;
vertical-align: middle;
transform: translate3d(0, 0, 0);
}
.cbx span:first-child {
position: relative;
width: 18px;
height: 18px;
border-radius: 4px;
transform: scale(1);
border: 1px solid #cccfdb;
transition: all 0.2s ease;
box-shadow: 0 1px 1px rgba(0, 16, 75, 0.05);
}
.cbx span:first-child svg {
position: absolute;
top: 3px;
left: 2px;
fill: none;
stroke: #fff;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
stroke-dasharray: 16px;
stroke-dashoffset: 16px;
transition: all 0.3s ease;
transition-delay: 0.1s;
transform: translate3d(0, 0, 0);
}
.cbx span:last-child {
padding-left: 8px;
line-height: 18px;
}
.cbx:hover span:first-child {
border-color: #07f;
}
.inp-cbx {
position: absolute;
visibility: hidden;
}
.inp-cbx:checked .cbx span:first-child {
background: #07f;
border-color: #07f;
animation: wave 0.4s ease;
}
.inp-cbx:checked .cbx span:first-child svg {
stroke-dashoffset: 0;
}
.inline-svg {
position: absolute;
width: 0;
height: 0;
pointer-events: none;
user-select: none;
}
@-moz-keyframes wave {
50% {
transform: scale(0.9);
}
}
@-webkit-keyframes wave {
50% {
transform: scale(0.9);
}
}
@-o-keyframes wave {
50% {
transform: scale(0.9);
}
}
@keyframes wave {
50% {
transform: scale(0.9);
}
}
<input class="inp-cbx" id="morning" type="checkbox" />
<label class="cbx" for="morning">
<span>
<svg width="12px" height="10px">
<use xlink:href="#check"></use>
</svg>
</span>
<span>Label</span>
</label>
<!--SVG Sprite-->
<svg class="inline-svg">
<symbol id="check" viewbox="0 0 12 10">
<polyline points="1.5 6 4.5 9 10.5 1"></polyline>
</symbol>
</svg>
當我提取單獨的 svg 并將其直接放在 svg 標記中(但洗掉了 inline-svg 類)時,檢查 svg 不再顯示。如此處所示:
* {
box-sizing: border-box;
}
body {
font-family: "Open Sans", sans-serif;
font-size: 16px;
-webkit-font-smoothing: antialiased;
text-rendering: optimizelegibility;
color: #223254;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.cbx {
-webkit-user-select: none;
user-select: none;
cursor: pointer;
padding: 6px 8px;
border-radius: 6px;
overflow: hidden;
transition: all 0.2s ease;
}
.cbx:not(:last-child) {
margin-right: 6px;
}
.cbx:hover {
background: rgba(0, 119, 255, 0.06);
}
.cbx span {
float: left;
vertical-align: middle;
transform: translate3d(0, 0, 0);
}
.cbx span:first-child {
position: relative;
width: 18px;
height: 18px;
border-radius: 4px;
transform: scale(1);
border: 1px solid #cccfdb;
transition: all 0.2s ease;
box-shadow: 0 1px 1px rgba(0, 16, 75, 0.05);
}
.cbx span:first-child svg {
position: absolute;
top: 3px;
left: 2px;
fill: none;
stroke: #fff;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
stroke-dasharray: 16px;
stroke-dashoffset: 16px;
transition: all 0.3s ease;
transition-delay: 0.1s;
transform: translate3d(0, 0, 0);
}
.cbx span:last-child {
padding-left: 8px;
line-height: 18px;
}
.cbx:hover span:first-child {
border-color: #07f;
}
.inp-cbx {
position: absolute;
visibility: hidden;
}
.inp-cbx:checked .cbx span:first-child {
background: #07f;
border-color: #07f;
animation: wave 0.4s ease;
}
.inp-cbx:checked .cbx span:first-child svg {
stroke-dashoffset: 0;
}
.inline-svg {
position: absolute;
width: 0;
height: 0;
pointer-events: none;
user-select: none;
}
@-moz-keyframes wave {
50% {
transform: scale(0.9);
}
}
@-webkit-keyframes wave {
50% {
transform: scale(0.9);
}
}
@-o-keyframes wave {
50% {
transform: scale(0.9);
}
}
@keyframes wave {
50% {
transform: scale(0.9);
}
}
<input class="inp-cbx" id="morning" type="checkbox" />
<label class="cbx" for="morning">
<span>
<svg width="12px" height="10px">
<symbol id="check" viewbox="0 0 12 10">
<polyline points="1.5 6 4.5 9 10.5 1"></polyline>
</symbol>
</svg>
</span>
<span>Label</span>
</label>
這種行為的原因是什么,我如何使它的行為方式相同?
我已經嘗試使用 inline-svg 類并檢查標簽以查找缺少 css 屬性的標簽,但我看不到有什么不同,這讓我相信這是 svg 標簽的一些特殊行為。
uj5u.com熱心網友回復:
在@enxaneta 的幫助下,我了解到<symbol>除非與 with 一起使用,否則它是不可見的,<use>所以我簡單地洗掉了<symbol>并用 a 替換它,<svg>它作業得很好:
* {
box-sizing: border-box;
}
body {
font-family: "Open Sans", sans-serif;
font-size: 16px;
-webkit-font-smoothing: antialiased;
text-rendering: optimizelegibility;
color: #223254;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.cbx {
-webkit-user-select: none;
user-select: none;
cursor: pointer;
padding: 6px 8px;
border-radius: 6px;
overflow: hidden;
transition: all 0.2s ease;
}
.cbx:not(:last-child) {
margin-right: 6px;
}
.cbx:hover {
background: rgba(0, 119, 255, 0.06);
}
.cbx span {
float: left;
vertical-align: middle;
transform: translate3d(0, 0, 0);
}
.cbx span:first-child {
position: relative;
width: 18px;
height: 18px;
border-radius: 4px;
transform: scale(1);
border: 1px solid #cccfdb;
transition: all 0.2s ease;
box-shadow: 0 1px 1px rgba(0, 16, 75, 0.05);
}
.cbx span:first-child svg {
position: absolute;
top: 3px;
left: 2px;
fill: none;
stroke: #fff;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
stroke-dasharray: 16px;
stroke-dashoffset: 16px;
transition: all 0.3s ease;
transition-delay: 0.1s;
transform: translate3d(0, 0, 0);
}
.cbx span:last-child {
padding-left: 8px;
line-height: 18px;
}
.cbx:hover span:first-child {
border-color: #07f;
}
.inp-cbx {
position: absolute;
visibility: hidden;
}
.inp-cbx:checked .cbx span:first-child {
background: #07f;
border-color: #07f;
animation: wave 0.4s ease;
}
.inp-cbx:checked .cbx span:first-child svg {
stroke-dashoffset: 0;
}
.inline-svg {
position: absolute;
width: 0;
height: 0;
pointer-events: none;
user-select: none;
}
@-moz-keyframes wave {
50% {
transform: scale(0.9);
}
}
@-webkit-keyframes wave {
50% {
transform: scale(0.9);
}
}
@-o-keyframes wave {
50% {
transform: scale(0.9);
}
}
@keyframes wave {
50% {
transform: scale(0.9);
}
}
<input class="inp-cbx" id="morning" type="checkbox" />
<label class="cbx" for="morning">
<span>
<svg width="12px" height="10px" viewbox="0 0 12 10">
<polyline points="1.5 6 4.5 9 10.5 1"></polyline>
</svg>
</span>
<span>Label</span>
</label>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/475350.html
