div设置了height:window.innerHeight但窗口变化时不起作用的解决代码

componentDidMount(){

window.addEventListener('resize',this.onWindowResize());

}

componentWillUnmount(){

window.removeEventListener('resize',this.onWindowResize());

}

onWindowResize(){

let newHeight = window.innerHeight-this.getHeight(100);

setTimeout(() => {

this.setState({containerHeight:newHeight})

},200);

}

转载于:https://my.oschina.net/u/268088/blog/1808399

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
function showPopup(imageSrc) { var popup = document.createElement("div"); var popupImg = document.createElement("img"); var scale = 1; var isDragging = false; var startX, startY, translateX, translateY; // 设置弹出窗口中的图片 popupImg.src = imageSrc; popupImg.style.transform = scale(${scale}); // 加载完成后计算图片的宽高比例 popupImg.onload = function() { var imgWidth = popupImg.width; var imgHeight = popupImg.height; // 计算图片的缩放比例 var windowWidth = window.innerWidth * 0.8; // 按照弹出窗口宽度的80%计算 var windowHeight = window.innerHeight * 0.8; // 按照弹出窗口高度的80%计算 var widthScale = windowWidth / imgWidth; var heightScale = windowHeight / imgHeight; scale = Math.min(widthScale, heightScale); // 设置弹出窗口中的图片样式 popupImg.style.transform = `scale(${scale})`; popupImg.style.display = "block"; popupImg.style.margin = "auto"; function centerPopup() { var windowWidth = window.innerWidth * 0.8; // 按照弹出窗口宽度的80%计算 var windowHeight = window.innerHeight * 0.8; // 按照弹出窗口高度的80%计算 var popupRect = popup.getBoundingClientRect(); var popupWidth = popupRect.width; var popupHeight = popupRect.height; scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; var offsetLeft = (windowWidth - popupWidth * scale) / 2; var offsetTop = (windowHeight - popupHeight * scale) / 2 + scrollTop; // 设置弹出窗口的位置 popup.style.left = offsetLeft + "px"; popup.style.top = offsetTop + "px"; } // 设置悬浮窗样式 function setPopupStyle() { var windowWidth = window.innerWidth * 0.8; // 按照弹出窗口宽度的80%计算 var windowHeight = window.innerHeight * 0.8; // 按照弹出窗口高度的80%计算 popup.style.position = "fixed"; popup.style.width = windowWidth + "px"; popup.style.height = windowHeight + "px"; popup.style.backgroundColor = "transparent"; popup.style.zIndex = "9999"; popup.onclick = function (event) { if (event.target === popup) { popup.style.display = "none"; } }; } // 在设置图片加载完成后调用居中弹出窗口设置悬浮窗样式的函数 popupImg.onload = function() { // 设置图片缩放比例 var imgWidth = popupImg.width; var imgHeight = popupImg.height; var widthScale = window.innerWidth * 0.8 / imgWidth; var heightScale = window.innerHeight * 0.8 / imgHeight; scale = Math.min(widthScale, heightScale); popupImg.style.transform = scale(${scale}); // 设置弹出窗口样式和居中位置 setPopupStyle(); centerPopup(); // 显示图片和悬浮窗 popupImg.style.display = "block"; popup.style.display = "block"; } // 添加图片到悬浮窗 popup.appendChild(popupImg); document.body.appendChild(popup);这段代码可以怎么优化
07-14
function showPopup(imageSrc) { var popup = document.createElement("div"); var popupImg = document.createElement("img"); var scale = 1; var isDragging = false; var startX, startY, translateX, translateY; // 设置弹出窗口中的图片 popupImg.src = imageSrc; popupImg.style.transform = `scale(${scale})`; // 加载完成后计算图片的宽高比例 popupImg.onload = function() { var imgWidth = popupImg.width; var imgHeight = popupImg.height; // 计算图片的缩放比例 var windowWidth = window.innerWidth * 0.8; // 按照弹出窗口宽度的80%计算 var windowHeight = window.innerHeight * 0.8; // 按照弹出窗口高度的80%计算 var widthScale = windowWidth / imgWidth; var heightScale = windowHeight / imgHeight; scale = Math.min(widthScale, heightScale); // 设置弹出窗口中的图片样式 popupImg.style.transform = `scale(${scale})`; popupImg.style.display = "block"; popupImg.style.margin = "auto"; // 居中弹出窗口 function centerPopup() { var windowHeight = window.innerHeight; var popupHeight = popup.offsetHeight; var scrollTop = window.pageYOffset || document.documentElement.scrollTop; var offset = (windowHeight - (imgHeight * scale)) / 2 + scrollTop; // 设置弹出窗口的垂直位置 popup.style.top = offset + "px"; } // 设置悬浮窗样式 popup.style.position = "fixed"; popup.style.top = "10%"; popup.style.left = "10%"; popup.style.width = windowWidth + "px"; popup.style.height = windowHeight + "px"; popup.style.backgroundColor = "transparent"; popup.style.zIndex = "9999"; // 添加图片到悬浮窗 popup.appendChild(popupImg); document.body.appendChild(popup); // 初始化居中弹出窗口 centerPopup(); // 添加滚动和调整窗口大小事件监听器 window.addEventListener("scroll", centerPopup); window.addEventListener("resize", centerPopup); // 点击事件监听器,点击其他区域隐藏弹出窗口 popup.onclick = function () { popup.style.display = "none"; }; // 移除滚动和调整窗口大小事件监听器 function removeListeners() { window.removeEventListener("scroll", centerPopup); window.removeEventListener("resize", centerPopup); } // 在窗口关闭移除事件监听器 window.onbeforeunload = function () { removeListeners(); }; };需要不管第几行第几列的图片放大,放大的悬浮窗始终在当前页面最中心
07-14
要在 Vue 中使 iro.js 控件自适应宽度和高度,您可以在初始化 iro.js 使用 `window.innerWidth` 和 `window.innerHeight` 属性设置宽度和高度。这是一个简单的示例: 1. 安装 iro.js: ```bash npm install iro --save ``` 2. 在 Vue 组件中引入 iro.js: ```javascript import iro from '@jaames/iro'; ``` 3. 添加一个 div 元素作为颜色选择器的容器,并在 Vue 组件的 `mounted` 生命周期函数中初始化 iro.js: ```html <template> <div> <div ref="colorPicker"></div> </div> </template> <script> import iro from '@jaames/iro'; export default { name: 'ColorPickerComponent', mounted() { this.initColorPicker(); }, methods: { initColorPicker() { const width = window.innerWidth; const height = window.innerHeight; const colorPickerScale = Math.min(width, height) * 0.8; const colorPicker = new iro.ColorPicker(this.$refs.colorPicker, { width: colorPickerScale, height: colorPickerScale }); colorPicker.on('input:change', (color) => { console.log('Color changed:', color.hexString); }); } } } </script> ``` 上述示例中,颜色选择器的宽度和高度将根据窗口大小自动缩放。为使颜色选择器在窗口大小改变自适应宽度和高度,可以添加 `window.onresize` 监听器并重新初始化颜色选择器。注意,在组件销毁,也需要清除监听器,以避免内存泄漏。这里是如何为颜色选择器添加 `resize` 监听器的示例: ```javascript export default { ... mounted() { this.initColorPicker(); window.addEventListener('resize', this.initColorPicker); }, beforeDestroy() { window.removeEventListener('resize', this.initColorPicker); }, ... } ``` 现在,当窗口大小改变,iro.js 颜色选择器会自动调整其宽度和高度。
05-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值