javascript - javascript 的窗口宽度条件

标签 javascript jquery

刚接触 js,仅在窗口大于 768 像素时才尝试应用某些代码行。

我想出了这个:

if ($(window).width() > 768) {
    const central = document.querySelector('.central');

    const tl = new TimelineMax();

    tl.fromTo(central,1,{height:'0vh'},{height:'80vh'})
    .fromTo(central,1,{width:'100vw'},{width:'80vw'});
 }

在 HTML 文件中加载这两行:

<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.1.3/TimelineMax.min.js" 
integrity="sha256-fIkQKQryItPqpaWZbtwG25Jp2p5ujqo/NwJrfqAB+Qk=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.1.3/TweenMax.min.js" 
integrity="sha256-lPE3wjN2a7ABWHbGz7+MKBJaykyzqCbU96BJWjio86U=" crossorigin="anonymous"></script>
<script src='js.js'></script>

如果没有if条件,它运行得很好,所以条件应用错误,你能帮我修复它吗?

最佳答案

将其与调整大小事件结合

$(window).resize(function() {
 if ($(window).width() > 768) {
    const central = document.querySelector('.central');

    const tl = new TimelineMax();

    tl.fromTo(central,1,{height:'0vh'},{height:'80vh'})
    .fromTo(central,1,{width:'100vw'},{width:'80vw'});
 }
}).resize();

添加了.resize(),它将在加载时调用window.resize事件。

关于javascript - javascript 的窗口宽度条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57476847/

相关文章:

javascript - 如何使用 Javascript 而不是 jQuery 来实现scrollTop 和toggleClass?

c# - 比赛 : Storing an arbitrary number of fields

javascript - VueJs 有像 React 这样的受控和非受控组件的概念吗?

javascript - fs.readFileSync 引用错误 : fileContents is not defined

javascript - jquery 检查表单是否为空白时出错

javascript - 如何让特殊字符在 d3.js 中正确呈现?

javascript - Bootstrap 列无法与 Kendo-UI 正常工作

jquery - 如何使 JQuery 将 Ajax 响应视为纯文本而不是 JSON

javascript - Cordova - JQuery Mobile 图标低分辨率

javascript - 创建jquery无限动画不使用递归?