【问题标题】:Resize a gridview's image with jquery使用 jquery 调整 gridview 的图像大小
【发布时间】:2016-04-11 14:40:23
【问题描述】:

我是 jquery 的新手,被困在某个地方。我有一个gridview,里面有一个图像列。尝试调整这些图像的大小,但真的无法弄清楚。尝试了这段代码,当图像不在网格视图中时它运行良好,但就像我说的我需要在网格视图中解决它。

这是我的网格:

<asp:GridView ID="GridViewFuel" AutoGenerateColumns="false" CssClass="mGrid" runat="server" DataSourceID="EntityDataSourceFuels">
    <Columns>
        <asp:TemplateField HeaderText="Sürücü">
            <ItemTemplate>
                <%#GetDriverNameAndSurname(Convert.ToInt16(Eval("DriverId"))) %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Araç">
            <ItemTemplate>
                <%#GetCarPlate(Convert.ToInt16(Eval("CarId"))) %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Fiş">
            <ItemTemplate>
                <asp:Image ID="Image1" ImageUrl='<%#Eval("FuelPrice") %>' runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

这是我的 jquery:

$(document).ready(function () {
        $('.mGrid img').each(function () {
            var maxWidth = 151; // Max width for the image
            var maxHeight = 151;    // Max height for the image
            var ratio = 0;  // Used for aspect ratio
            var width = $(this).width();    // Current image width
            var height = $(this).height();  // Current image height

            // Check if the current width is larger than the max
            if (width > maxWidth) {
                ratio = maxWidth / width;   // get ratio for scaling image
                $(this).css("width", maxWidth); // Set new width
                $(this).css("height", height * ratio);  // Scale height based on ratio
                height = height * ratio;    // Reset height to match scaled image
                width = width * ratio;    // Reset width to match scaled image
            }

            // Check if current height is larger than max
            if (height > maxHeight) {
                ratio = maxHeight / height; // get ratio for scaling image
                $(this).css("height", maxHeight);   // Set new height
                $(this).css("width", width * ratio);    // Scale width based on ratio
                width = width * ratio;    // Reset width to match scaled image
            }
        });
    });

如何使它与我的 gridview 一起工作?

【问题讨论】:

    标签: jquery asp.net gridview


    【解决方案1】:

    我会尝试遍历每个单元格。如果存在图像,则按比例计算。

    $('.mGrid tr').each(function () {

    $('td', this).each(function () {
    
        //check if image exists
    
     });
    

    })

    【讨论】:

    • 就像我说的我是 jquery 的新手,所以如果你能展示我应该如何检查 td 是否是图像,那就太棒了?
    • 循环遍历每个单元格后,您需要检查图像是否存在。我自己没有测试它,但它应该类似于以下内容: var img = $(this).find('img'); if (img.length > ) { // 与 img 做比率 }
    猜你喜欢
    • 2014-10-12
    • 2011-08-17
    • 2018-03-28
    • 2010-11-11
    • 1970-01-01
    • 2011-03-16
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多