请告诉我如何才能正常工作
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/