欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 幼教 > vue 页面自定义指令修改after 样式 背景图片等

vue 页面自定义指令修改after 样式 背景图片等

2025/9/23 7:43:06 来源:https://blog.csdn.net/weixin_50601484/article/details/144903580  浏览:    关键词:vue 页面自定义指令修改after 样式 背景图片等

vue 自定义指令修改after 背景图片

维护老项目 我的需求是根据不同地区在after上加载不同地图

// 新建自定义指令 和data同级
directives: {after: {inserted(el, binding) {}}
}
// 使用自定义指令
<div v-after="cityName" class="city-other"></div>
// 自定义指令内容
const list = {酒泉市: 'jq',嘉峪关市: 'jyg',武威市: 'ww',兰州市: 'lz',张掖市: 'zy',金昌市: 'jc',临夏回族自治州: 'lxhz',定西市: 'dx',庆阳市: 'qy',白银市: 'by',天水市: 'ts',陇南市: 'ln',甘南藏族自治州: 'gnzz',平凉市: 'pl'
};
const style = document.createElement('style');
style.type = 'text/css';
// 定义伪元素的样式
const afterStyles = `.city-other::after {position: absolute;z-index: 1;content: '';top: 87px;right: 100px;width: 828px;height: 730px;background: url(${require('../../assets/home/map/' +list[binding.value] +'.png')}) no-repeat center;background-size: contain;}
`;
// 将样式添加到 <style> 元素中
if (style.styleSheet) {// 针对 IE 浏览器style.styleSheet.cssText = afterStyles;
} else {style.appendChild(document.createTextNode(afterStyles));
}
// 将 <style> 元素添加到文档的 <head> 中
document.head.appendChild(style);

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词