javascript - 如何制作具有动态内容的可滚动 div

标签 javascript jquery html

当我单击添加按钮时,我试图将图像添加到我的 div 中从下到上。现在它可以工作了,但我的问题是 div 不滚动。我做错了什么?

JSFiddle link

HTML

 <button id="add_content">Add</button>
 <div class="image_panel"></div>

CSS

  .image_panel {
     width:100%;
     height:300px;
     border:1px solid red;
     overflow-y:scroll;
  }
  #add_content{
     float:left;
  }

JavaScript

$(document).ready(function() {
  var smallimg_count=0;
  var bottom=0;
  $("#add_content").click(function() {
    smallimg_count++;
    bottom=bottom+20;      
    var insert_html_small = '<div id="imageGrid_' + smallimg_count + '"  class="imageGrid_small"  >
                               <img class="resize1" id="resize_' + smallimg_count + '" src="http://webbies.dk/assets/templates/SudoSlider/images/toolbox_designer.png" style="bottom:' + bottom + 'px;" />
                             </div>';
    $(insert_html_small).appendTo(".image_panel");
  });
});

最佳答案

我看到的一个问题是您要添加的图像具有绝对位置内联样式(来自您的 JSFiddle)

例如

style="bottom:' +   bottom + 'px; position: absolute;'

因此它与 div 处于不同的堆叠上下文中

删除它似乎可以使其工作:http://jsfiddle.net/4Ftev/12/

例如

$(document).ready(function () {
    var smallimg_count = 0;
    var bottom = 0;
    $("#add_content").click(function () {
        smallimg_count++;
        bottom = bottom + 20;
        var insert_html_small = '<div id="imageGrid_' + smallimg_count + '"  class="imageGrid_small"  ><img   class="resize1" id="resize_' + smallimg_count + '" src="http://webbies.dk/assets/templates/SudoSlider/images/toolbox_designer.png" /></div>';
        $(insert_html_small).appendTo(".image_panel");
    });
});

如果你想从下往上添加,那么你可以使用prependTo,参见这个JSFiddle

http://jsfiddle.net/4Ftev/15/

$(document).ready(function () {
    var smallimg_count = 0;
    var bottom = 0;
    $("#add_content").click(function () {
        smallimg_count++;
        bottom = bottom + 20;
        var insert_html_small = '<div id="imageGrid_' + smallimg_count + '"  class="imageGrid_small"  >' + smallimg_count + '<img   class="resize1" id="resize_' + smallimg_count + '" src="http://webbies.dk/assets/templates/SudoSlider/images/toolbox_designer.png" /></div>';
        $(insert_html_small).prependTo(".image_panel");
    });
});

关于javascript - 如何制作具有动态内容的可滚动 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16047002/

相关文章:

javascript - 如何将光标位置放在 contenteditable 元素的居中对齐占位符的开头?

javascript - 如何获取元素周围的文本?

jquery - 如何使用 jquery 触发类和悬停效果

Java 通过 jquery 传递数组

javascript - div 内带有 Backbone 的 Handlebars 模板不渲染

javascript - 如何在if语句中使用ajax调用成功后返回数据

javascript - 如何过渡堆叠的 Div。在 JavaScript 中

javascript - 如何用div缩放div的内容?

javascript - 如何同时给两个css类名,第二个在页面呈现后被删除?

javascript - 具有页面完整浏览器高度的开头部分,所有其他部分定义的高度。