jQuery - create an element with 16:9 aspect ratio

心不动则不痛 提交于 2019-12-12 10:16:14

问题


I am actually working on an image gallery which contains thumbnails of some videos I made. For this, I calculate the with of the gallery through the following code.

$(function() {
$(window).resize(function() {
    var width = $(this).width() - 200;
    $("#gallery").css("width", width);
}).resize();});

Now each image should have an 16:9 aspect ratio. Therefore, I have to divide the width of the gallery width 16 and multiply this value with 9. Doesn't seem to be that hard but actually I am stuck. Hopefully someone can help me, thanks!


回答1:


You should recalculate the element height:

$(function() {
    $(window).resize(function() {
        var width = $(this).width() - 200;
        $("#gallery").css({
            "width": width,
            "height": width*(9/16)
        });
    }).resize();
});

Here's a proof-of-concept fiddle: http://jsfiddle.net/teddyrised/w555h/6/




回答2:


Try this:

$(function() {
$(window).resize(function() {
    var width = $(this).width() - 200;
var ratio = 16 / 9.0;
    $("#gallery").css("width", width).css("height", Math.round(width * ratio));
}).resize();});


来源:https://stackoverflow.com/questions/19747690/jquery-create-an-element-with-169-aspect-ratio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!