用vue 做一个 ctrl加滚轮滑动 页面最小的百分比是100%
时间: 2024-03-02 16:52:15 浏览: 19
你可以这样编写一个 Vue 指令来实现这个功能:
```html
<template>
<div v-resize="{minHeight: 100}">
<!-- Content goes here -->
</div>
</template>
<script>
export default {
directives: {
resize: {
inserted(el, binding) {
let minHeight = binding.value.minHeight || 100;
let height = window.innerHeight;
el.style.minHeight = `${Math.max(minHeight, height)}px`;
window.addEventListener("resize", () => {
height = window.innerHeight;
el.style.minHeight = `${Math.max(minHeight, height)}px`;
});
window.addEventListener("wheel", (e) => {
if (e.ctrlKey) {
e.preventDefault();
let delta = Math.sign(e.deltaY) * 10;
height = Math.max(minHeight, height - delta);
el.style.minHeight = `${height}px`;
}
});
},
},
},
};
</script>
```
这个指令会在页面加载时自动设置 `div` 元素的最小高度为窗口高度,或者指定的最小高度。当用户按住 Ctrl 键滚动鼠标滚轮时,会动态调整 `div` 元素的最小高度。注意,这个指令只会在用户按住 Ctrl 键时才会生效。