javascript - 拖入 GIF 时表格变形

标签 javascript css html-table

当我将 gif 拖入单元格时,为什么表格会变形?我该如何解决这个问题?当我将 gif 拖入单元格时,我希望表格(和每个单元格)保持原来的大小。

这是我的情况的一个jsfiddle: http://jsfiddle.net/MjMcr/3/

<body>
<img id="drag1" src="http://files.myopera.com/supersagitta/Pokemon/BW_Sprites/Pikachu.gif" draggable="true" ondragstart="drag(event)">
<div id="play1">
    <table border="1" class="play">
        <tr>
            <td>
                <div id="div1" class="room left" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
            </td>
            <td>
                <div id="div2" class="room center" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
            </td>
            <td>
                <div id="div3" class="room right" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
            </td>
        </tr>
        <tr>
            <td>
                <div id="div4" class="room left" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
            </td>
            <td>
                <div id="div5" class="room center" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
            </td>
            <td>
                <div id="div6" class="room right" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
            </td>
        </tr>
    </table>
</div>

CSS

.play {
width: 100%;
height: 100%;
}
.play td {
}
.play .room {
    width:100%;
    height:100%;
    border:1px solid #aaaaaa;
}
.left{
    background-color:blue;
}
.center{
    background-color:white;
}
.right{
    background-color:red;
}

JavaScript

    function allowDrop(ev) {
    ev.preventDefault();
}

function drag(ev) {
    ev.dataTransfer.setData("Text", ev.target.id);
}

function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("Text");
    ev.target.appendChild(document.getElementById(data));
}

最佳答案

只需将 table-layout:fixed; 添加到表格中,并为行添加 height: 50%;:

http://jsfiddle.net/MjMcr/4/

这解决了您的问题 - 但您应该注意,正如评论中所述,您根本不应该使用表格来构建它。表格用于表格数据,而不是构建布局。也许你不想读this question on SO ("Why not use tables for layout in HTML?")

关于javascript - 拖入 GIF 时表格变形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17153080/

相关文章:

html - 良好的表格实践 - 在表格中制作标题的最佳方式?

javascript - (如何)我们可以创建带有范围标签的半圆环图吗?

c# - 在 ASP.NET 中检测关闭浏览器

css - 如何添加:hover affects to fa-fa icons?

html - webkit-scrollbar 在 iOS safari 中留下轨迹线

php - PHP 中的分组查询结果

javascript - 在 JavaScript 中单击其中一个单选按钮时启用按钮

javascript - 如何使用 VaadinServlet 为图像设置 RequestHandler

html - 我怎样才能让这个菜单的 "clear:both"在谷歌浏览器中工作?

html - 可以使固定标题继承宽度属性