Jquery .css 从当前大小缩放图像

标签 jquery css image scale

我正在尝试根据当前大小而不是原始大小调整图像大小,因为当屏幕尺寸发生变化时(使用@media screen.. {},我已经对图像(在 css 文件中)进行了不同的变换属性,所以它会在不同尺寸的屏幕上看起来是一样的)。

//..this is only part of a code of a jslider..

callback: function( value ){
    var SliderValue = $('#SliderSingle2').attr('value');
    if(value== "2003"){                         
        //intresting part here:
        var currentSize = $("#img").css('transform');
        var currentSize = parseFloat(currentSize)*1.2;
        $("#img").css('transform', currentSize);
        alert(currentSize);
    }
}

//...that alert returns now NaN so parseFloat can't read 
//transform:scale(0.6) attribute.. but there isn't parseDouble option.. 
//Any succestions??

我想做的是:( link ) 仅使用图像,而不是字体..

最佳答案

您在警报中获得 NaN 值的原因是:

var currentSize = $('#img').css('transform'); //Returns a string. Example: "matrix(a,b,c,d,tx,ty)"
parseFloat(currentSize); // NaN

更多相关信息:CSS Transform on MDN

您需要计算将返回的矩阵转换为可用值所需的数学运算。然后将调整后的值反馈给 jQuery CSS 方法。另一种选择是尝试这样的事情:

var imgWidth = Math.round($('#img').css('width').replace('px','')*1.2),
    imgHeight = Math.round($('#img').css('height').replace('px','')*1.2);

$('#img').css({ width:imgWidth, height:imgHeight });

希望这对您有所帮助。

关于Jquery .css 从当前大小缩放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16829902/

相关文章:

javascript - 为什么在 crossOrigin = 'Anonymous' 图像上设置 base64 数据时 Safari 会抛出 CORS 错误?

jquery - 将变量从 Google map 传递到 jQuery

javascript - 循环中的多个 Javascript 延迟

asp.net - Css 验证在 asp 网络中不起作用

javascript - jQuery:仅在特定浏览器宽度下才转换

图像二进制解释 : unknown image format

javascript - 在 HTML 中叠加图像

jquery - 修复旋转父项内可拖动 div 的鼠标方向

jquery datatables - 对组内的列求和

javascript - javascript 中的颜色相等