这里使用到vue的自定义指令
javascript
<div class="item" v-clickoutside="clickoutside1">
<div @click="opencity" class="text"
:style="{ color: popup.iscitypop || okcitylist.length != 0 ? '#FF9500' : '#000000' }">选择地区
</div>
<i v-if="popup.iscitypop || okcitylist.length != 0" class=" el-icon-arrow-up"
:style="{ color: popup.iscitypop || okcitylist.length != 0 ? '#FF9500' : '#000000' }"></i>
<i v-else class="el-icon-arrow-down"
:style="{ color: popup.iscitypop || okcitylist.length != 0 ? '#FF9500' : '#000000' }"></i>
//绝对定位样式 popup.iscitypop true展示 false隐藏
<div class="poppad" v-if="popup.iscitypop">
<div class="tit" v-for="(item, index) in citylist" :key="index">
{{ item.n }}
<div class="cit">
<div class="items" :class="its.isactive ? 'active' : ''" v-for="(its, ind) in item.s" :key="ind"
@click.stop="changeCity(its, ind)">
{{ its.n }}
</div>
</div>
</div>
</div>
</div>
export default {
directives: {
clickoutside: {
bind: function (el, binding, vnode) {
function documentHandler(e) {
if (el.contains(e.target)) {
return false;
}
if (binding.expression) {
binding.value(e);
}
}
el.__vueClickOutside__ = documentHandler;
document.addEventListener("click", documentHandler);
},
unbind: function (el, binding) {
document.removeEventListener("click", el.__vueClickOutside__);
delete el.__vueClickOutside__;
}
}
},
}