jquery - 如果元素宽度超过 80%

标签 jquery width

请告诉我如何才能正常工作

var $photoblock = $(".element img");
if ($photoblock).width() > 80%){
$photoblock.css({width: 80%, max-width: 'none'});
} else {
$photoblock.css({width: auto, max-width: 100%});
});

怎样才是正确的?

最佳答案

尝试这样的事情

var $photoblock = $(".element img");
var per = $photoblock.parent().width() * 0.8;
if ($photoblock.width() > per) {
    $photoblock.css({
        width: '80%',
        'max-width': 'none'
    });
} else {
    $photoblock.css({
        width: 'auto',
        'max-width': '100%'
    });
}

$photoblock.width() 将返回像素宽度,而不是百分比。因此你需要找出它的父级的 80%,然后需要比较它。

关于jquery - 如果元素宽度超过 80%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21043561/

相关文章:

javascript - C# 和 JS 中的 WebSocketServers - 乱码

css - 覆盖并重置 CSS 样式 : auto or none don't work

c# - 在 iTextSharp 中操作行和单元格的宽度和高度

wpf - 如何使另一个 StackPanel 的 StackPanel 宽度?

php - 如何PHPExcel设置自动列宽

Javascript 移动设备宽度检查和类删除

php - jquery POST方法回调函数查询

php - jquery 构建数组不起作用

javascript - 使用 .then 进行包装在函数中的 ajax 调用

javascript - 如何使用 jquery 启用禁用的事件处理程序