防止在内联 svg 上单击附近的文本选择
Prevent nearby text selection on inline svg click
当使用内联 SVG 作为可点击元素时,点击它两次可能 select 附近的文本。
如何在不强制 user-select 对所有附近元素的情况下避免这种情况?
.wrap {
height: 100px;
}
.icon {
height: 100%;
pointer-events: bounding-box;
cursor: pointer;
}
<div class="wrap">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 136 136">
<defs/>
<path fill="none" stroke="rgb(160,70,255)" stroke-width="3" d="M68 134.43A66.43 66.43 0 101.5 68 66.46 66.46 0 0068 134.43zM123.77 68H12.23M68 12.3v111.41"/>
</svg>
</div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
这是你的意思吗? (已添加用户-select:none;)
.wrap {
height: 100px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.icon {
height: 100%;
pointer-events: bounding-box;
cursor: pointer;
}
<div class="wrap">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 136 136">
<defs/>
<path fill="none" stroke="rgb(160,70,255)" stroke-width="3" d="M68 134.43A66.43 66.43 0 101.5 68 66.46 66.46 0 0068 134.43zM123.77 68H12.23M68 12.3v111.41"/>
</svg>
</div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
当使用内联 SVG 作为可点击元素时,点击它两次可能 select 附近的文本。
如何在不强制 user-select 对所有附近元素的情况下避免这种情况?
.wrap {
height: 100px;
}
.icon {
height: 100%;
pointer-events: bounding-box;
cursor: pointer;
}
<div class="wrap">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 136 136">
<defs/>
<path fill="none" stroke="rgb(160,70,255)" stroke-width="3" d="M68 134.43A66.43 66.43 0 101.5 68 66.46 66.46 0 0068 134.43zM123.77 68H12.23M68 12.3v111.41"/>
</svg>
</div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>
这是你的意思吗? (已添加用户-select:none;)
.wrap {
height: 100px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.icon {
height: 100%;
pointer-events: bounding-box;
cursor: pointer;
}
<div class="wrap">
<svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 136 136">
<defs/>
<path fill="none" stroke="rgb(160,70,255)" stroke-width="3" d="M68 134.43A66.43 66.43 0 101.5 68 66.46 66.46 0 0068 134.43zM123.77 68H12.23M68 12.3v111.41"/>
</svg>
</div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit</div>