【问题标题】:Dragscroll a div containing floated divs拖动滚动包含浮动 div 的 div
【发布时间】:2015-09-20 11:27:00
【问题描述】:

我正在尝试拖动滚动包含浮动元素的 div。你可以玩here

意图是拖动灰色区域应该拖动窗格。我已经应用了类似“将 div 扩展到浮动内容”问题的建议。这是我的最大努力 - 垂直溢出看起来不错,但水平滚动却不行。

  1. 在浮动元素的末尾添加了一个透明元素
  2. 添加了“溢出:隐藏;”浮动元素的父级
  3. 尝试浮动父 div,但这似乎无法解决问题

设置固定宽度有效,但内容是动态的。

代码

<div class="title">Tall elements - ok</div>

<div id="wrapper">
    <div id="scroller">
        <div id="items">
            <div class="item-tall">hi</div>
            <div class="item-tall">ho</div>
            <div class="item-tall">off</div>
            <div class="item-tall">...</div>
            <div class="clear"></div>
         </div>
    </div>
</div> 

<div class="title">Wide elements - not ok</div>

<div id="wrapper2">
    <div id="scroller2">
        <div id="items2">
            <div class="item-wide">hi</div>
            <div class="item-wide">ho</div>
            <div class="item-wide">off</div>
            <div class="item-wide">...</div>
            <div class="clear"></div>
         </div>
    </div> 
</div>

$('#wrapper, #scroller').dragscrollable({
    dragSelector: '#items', 
    acceptPropagatedEvent: false
});

$('#wrapper2, #scroller2').dragscrollable({
    dragSelector: '#items2', 
    acceptPropagatedEvent: false
});

    #wrapper {
    width: 220px;
    height: 200px;
    overflow: auto;
    border: 1px solid #ff0000;    
    background-color: lightgray;
    cursor: all-scroll;    
}

#scroller {
    height: 100%;
}

#wrapper2 {
    width: 220px;
    height: 200px;
    overflow: auto;
    border: 1px solid #ff0000;    
    background-color: lightgray;
    cursor: all-scroll;
}

#scroller2 {
    height: 100%;    
}

#items {
    overflow: hidden;    
}

#items2 {
    height: 100%;
    /* width: 500px; this will fix it */
    overflow: hidden;      
}

.item-tall {
    width: 30px;
    height: 500px;
    float: left;     
    background-color: white;
    cursor: default;
}

.item-wide {
    height: 30px;
    min-width: 1000px;
    float: left;   
    background-color: white;
    cursor: default;
}

.clear {
    clear: both;
}

.title {
    padding: 20px;
}

参考文献

  1. Horizontal scroll in a parent div containing floated child divs

  2. Floating elements within a div, floats outside of div. Why?

【问题讨论】:

    标签: javascript html css scroll css-float


    【解决方案1】:

    那么,你想要什么? 水平滚动条不应该出现在您的第二项中吗? 为此,不要在第二个包装器中分配宽度。您已在第二个包装器中分配了 width:220px;,但只有一个孩子有 width:300px ,即大于父宽度这就是水平滚动条出现的原因。 不要为 wrapper2....

    使用宽度
    #wrapper2 {
    
        height: 200px;
        overflow: auto;
        border: 1px solid #ff0000;    
        background-color: lightgray;
        cursor: all-scroll;
    }
    

    我希望这行得通。

    【讨论】:

    • 不,滚动条是示例的有意部分。目标是可以通过在灰色区域上单击拖动来滚动 div。可以通过在 items2 上设置固定宽度来实现所需的效果,例如jsfiddle.net/QhWbm/101 但我们不能这样做。
    猜你喜欢
    • 2013-04-08
    • 1970-01-01
    • 2012-08-11
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    相关资源
    最近更新 更多