如何使用动态内容制作可滚动的div

时间:2013-04-16 21:01:29

标签: javascript jquery html

当我点击添加按钮时,我试图在底部到顶部的内部添加图像。现在它正在工作,但我的问题是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");
  });
});

3 个答案:

答案 0 :(得分:1)

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

e.g。

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

因此它处于与div

不同的堆叠上下文中

删除它似乎使其有效:http://jsfiddle.net/4Ftev/12/

e.g。

$(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");
    });
});

答案 1 :(得分:0)

您使用的是绝对定位的图像,位于不相对定位的div内。

首先,将position:relative;添加到.image_panel类样式

您现在会注意到的是,图像是相对于div定位的。 如果你想测试滚动条,我建议你使用边距而不是图像上的绝对定位。

赞,margin-top:900px;

<强>的CSS

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

<强>的Javascript

        $(document).ready(function(){
var smallimg_count=0;
var bottom=500;
$("#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="margin-top:' +   bottom + 'px;"    /></div>';
$(insert_html_small).appendTo(".image_panel");
});
});

http://jsfiddle.net/3gzaW/

祝你好运。 纳斯

答案 2 :(得分:0)

您的图片:

<img class="resize1" id="resize_3" src="http://webbies.dk/assets/templates/SudoSlider/images/toolbox_designer.png" style="bottom:60px; position: absolute;;">

取消职位:绝对; (使其相对,或完全删除)

相关问题