javascript - 根据浏览器宽度动态设置插件属性

标签 javascript jquery

我目前正在实现 accordion image slider到项目中, slider 具有以下可选设置。

$("#accordion").awsAccordion({
    type: "horizontal",
    cssAttrsHor: {
        ulWidth: "responsive",
        liHeight: 500,
        liWidth: 50
    },
    cssAttrsVer: {
        ulWidth: "responsive"
    },
    startSlide: 2,
    openCloseHelper: {
        openIcon: "plus",
        closeIcon: "minus"
    },
    openOnebyOne: true,
    classTab: "small",
    slideOn: "click",
    autoPlay: true,
    autoPlaySpeed: 3000
})

其中显示:键入:“水平”, 我想将其更改为: type:"vertical", 当浏览器宽度低于 768px 时,这是否可能。

最佳答案

这可以通过使用变量设置type属性来完成

var type = 'horizontal'; // Default type
if ($(window).width() < 768) {
    type = 'vertical'; // When window width is less than 768, make type vertical
}

$("#accordion").awsAccordion({
    type: type, // Use variable here
    ...
});

要在窗口调整大小时运行代码,可以使用 resize 事件。

function update() {
    var type = 'horizontal'; // Default type
    if ($(window).width() < 768) {
        type = 'vertical'; // When window width is less than 768, make type vertical
    }

    $("#accordion").awsAccordion({
        type: type, // Use variable here
        ...
    });
}

$(window).resize(update).trigger('resize');

关于javascript - 根据浏览器宽度动态设置插件属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36416301/

相关文章:

javascript - 如何防止容器中的排序操作?

javascript - 表排序器分页使表行不可点击

javascript - 使用 window.open 后更改 HTML 属性

javascript - 如何在数据表服务器端处理中加载额外的Javascript?

javascript - 如何在网页上定位一个 setattribute 值并换行文本,这样按钮就不会移动

php - 使用 PHP 和 Javascript 从 MySQL 加载数据的最佳方法

javascript - JS 获取带有表格内容的 HTML `<template>` innerHTML 失败

javascript - 使用替换后 appendChild() 将不起作用

javascript - React Native 演示中的代码风格是什么?

javascript - 将多行从 HTML 发送到 PHP 并存储在 JSON 中