javascript - img 调整大小,但宽度将停止在 600

标签 javascript jquery html css

HTML

<div>
    <div style="overflow: auto">
        <img class="preview_img" src="/myImg" />
        <img class="preview_img" src="/myImg2" />
    </div>
</div>

JS

btnZoomIn.click(function () {
    previewImg.each(function () {
        var width = $(this).width();
        var height = $(this).height();

        var newWidth = width * 1.2;
        var newHeight = height * 1.2;

        $(this).css('width', newWidth);
        $(this).css('height', newHeight);
        // $(this).width(newWidth);
        // $(this).height(newHeight);
    });
});

我想在点击按钮时调整 img 的大小,宽度将停在 600px,

但高度可以无限增加。

我没有设置任何最大宽度属性(属性)

最佳答案

可能从其他元素继承max-width,所以添加内联样式来防止max-width规则:

previewImg.each(function () {
    $(this).css('max-width','none');
});

btnZoomIn.click(function () {

    previewImg.each(function () {
        var width = $(this).width();
        var height = $(this).height();

        var newWidth = width * 1.2;
        var newHeight = height * 1.2;

        $(this).css('width', newWidth);
        $(this).css('height', newHeight);
        // $(this).width(newWidth);
        // $(this).height(newHeight);
    });
});

然后,如果存在问题,请检查父级的 max-widthwidth

关于javascript - img 调整大小,但宽度将停止在 600,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44341158/

相关文章:

javascript - 选择类别选项在提交时未检测到更改的尺寸?

javascript - grunt 构建后 Angular 模块不可用

javascript - jquery 单击事件未在 Internet Explorer 上触发

c# - LiteralControl 出现意外标记 "}"错误

javascript - Fancybox 在第二次点击时响应并且不切换到下一张图片

python scrapy - 从 <table> 中提取数据 - 没有 id 标签

javascript - 第二次单击标记时不会打开传单弹出窗口

javascript - 如何将点击的视频 block 打开到弹出模式?

javascript - 通过 jQuery 为将来创建的元素添加 CSS 规则

python - 文本信息无法正确抓取-Python