2017-02-22 118 views
0

我有可編輯的圖像元素。當光標離開包裝器時,在其中寫入文本時,它會滾動並放置在視圖中。如何解決它(如果可能的話,只使用CSS,不要更改結構)。防止contenteditable自動滾動

.wrap { 
 
    position: relative; 
 
    border: 1px solid red; 
 
    width: 70%; 
 
    margin: 0 auto; 
 
    overflow: hidden; 
 
} 
 

 
.wrap .item { 
 
    position: absolute; 
 
    white-space: nowrap; 
 
    left: 70%; 
 
    top: 50%; 
 
    background: rgba(20, 20, 20, .5); 
 
    color: white; 
 
    padding: 10px 15px; 
 
    display: inline-block; 
 
} 
 

 
img { 
 
    width: 100%; 
 
    display: block; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="wrap"> 
 
    <img src="http://placehold.it/350x150"/> 
 
    <div class="item" contenteditable="true">Write HERE to get very long text and see scroll</div> 
 
</div>

+0

所以你要編輯的div留在包裝界即使文字太長? – Armin

+0

@Armin是的,如果溢出 - 不滾動整個包裝,只是保持無敵。 – Justinas

+0

我更新了我的代碼。 – Armin

回答

0

使用位置是:固定;而不是絕對的,將停止滾動,如下

.wrap { 
 
    position: relative; 
 
    border: 1px solid red; 
 
    width: 70%; 
 
    margin: 0 auto; 
 
    overflow: hidden; 
 
} 
 

 
.wrap .item { 
 
    position: fixed; 
 
    white-space: nowrap; 
 
    left: 70%; 
 
    top: 50%; 
 
    background: rgba(20, 20, 20, .5); 
 
    color: white; 
 
    padding: 10px 15px; 
 
    display: inline-block; 
 
} 
 

 
img { 
 
    width: 100%; 
 
    display: block; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="wrap"> 
 
    <img src="http://placehold.it/350x150"/> 
 
    <div class="item" contenteditable="true">Write HERE to get very long text and see scroll</div> 
 
</div>

+0

設置'pos:fixed'將使元素彈出包裝,並且滾動將無法保留在父項的相同位置。 – Justinas

0

只要給像素值CONTENTEDITABLE div的寬度。

.wrap { 
 
    position: relative; 
 
    border: 1px solid red; 
 
    width: 70%; 
 
    margin: 0 auto; 
 
    overflow: hidden; 
 
} 
 

 
.wrap .item { 
 
    position: absolute; 
 
    white-space: nowrap; 
 
    left: 70%; 
 
    top: 50%; 
 
    width:100px; 
 
    background: rgba(20, 20, 20, .5); 
 
    color: white; 
 
    padding: 10px 15px; 
 
    display: inline-block; 
 
    overflow-x: auto; 
 
} 
 

 
img { 
 
    width: 100%; 
 
    display: block; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="wrap"> 
 
    <img src="http://placehold.it/350x150"/> 
 
    <div class="item" contenteditable="true">Write HERE to get very long text and see scroll</div> 
 
</div>

+0

無法將其設置爲固定大小,它必須是動態的。 – Justinas

+0

@Justinas你可以動態給予價值,但價值單位應該在'px' – Sankar

1

這是我更新的代碼。行不會打破,而圖像不會滾動:

.wrap { 
 
    position: relative; 
 
    border: 1px solid red; 
 
    width: 70%; 
 
    margin: 0 auto; 
 
    overflow: hidden; 
 
} 
 

 
.wrap .item { 
 
    position: absolute; 
 
    white-space: nowrap; 
 
    left: 70%; 
 
    top: 50%; 
 
    background: rgba(20, 20, 20, .5); 
 
    color: white; 
 
    padding: 10px 15px; 
 
    display: inline-block; 
 
    right: 0; 
 
    overflow: hidden; 
 
} 
 

 
img { 
 
    width: 100%; 
 
    display: block; 
 
    position: sticky; 
 
    top: 0; left: 0; right: 0; bottom: 0; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="wrap"> 
 
    <img src="http://placehold.it/350x150"/> 
 
    <div class="item" contenteditable="true">Write HERE to get very long text and see scroll</div> 
 
</div>

+0

它不能是自動分解的文本。此外,我不想讓它始終可見,只是不要滾動圖像打字單行 – Justinas

+0

@Justinas對不起,我誤解了。你可以檢查我的更新版本嗎? – Armin

1

通過@Julian啓發D. awesome hack 還不夠完善一個解決方案,有一些奇怪的閃爍,顯示由於在某一時刻到不透明度的切換。

body { 
 
    overflow: hidden; 
 
} 
 

 
.wrap { 
 
    width: 70%; 
 
    margin: 0 auto; 
 
    overflow: hidden; 
 
} 
 

 
.wrap .item { 
 
    position: absolute; 
 
    white-space: nowrap; 
 
    left: 70%; 
 
    top: 50%; 
 
    background: rgba(20, 20, 20, .5); 
 
    color: white; 
 
    padding: 10px 15px; 
 
    display: inline-block; 
 
    max-width: 60px; 
 
} 
 

 
img { 
 
    width: 100%; 
 
    display: block; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script> 
 
    $(function() { 
 
    var editableElement = $('#editable'), clonedElement; 
 

 
    // Revert any scrolling      
 
    editableElement.on("scroll", function(event) { 
 
     editableElement.scrollTop(0); 
 

 
     // Try to prevent scrolling completely (doesn't seem to work) 
 
     event.preventDefault(); 
 
     return false; 
 
    }); 
 

 
    // Switch overflow visibility on and off again on each keystroke. 
 
    // To avoid flickering, a cloned element is positioned below the input 
 
    // and switched on while we hide the overflowing element. 
 
    editableElement.on("keydown", function() { 
 

 
     // Create a cloned input element below the original one 
 
     if (!clonedElement) { 
 
     var zIndex = editableElement.css('zIndex'); 
 
     if (isNaN(parseInt(zIndex, 10))) { 
 
      zIndex = 10; 
 
      editableElement.css({zIndex: zIndex}); 
 
     }  
 

 
     clonedElement = editableElement.clone(); 
 
     clonedElement.css({ 
 
      zIndex: zIndex-1, 
 
      position: 'absolute', 
 
      top: editableElement.offset().top, 
 
      left: editableElement.offset().left, 
 
      overflow: 'hidden', 
 
      // Set pseudo focus highlighting for webkit 
 
      // (needs to be adapted for other browsers) 
 
      outline: 'auto 5px -webkit-focus-ring-color' 
 
     }); 
 
     editableElement.before(clonedElement); 
 
     } else { 
 
     // Update contents of the cloned element from the original one 
 
     clonedElement.html(editableElement.html()); 
 
     } 
 

 
     // Here comes the hack: 
 
     // - set overflow visible but hide element via opactity. 
 
     // - show cloned element in the meantime 
 
     clonedElement.css({opacity: 1}); 
 
     editableElement.css({overflow: 'visible', opacity: 0}); 
 

 
     // Immediately turn of overflow and show element again. 
 
     setTimeout(function() { 
 
     editableElement.css({overflow: 'hidden', opacity: 1}); 
 
     clonedElement.css({opacity: 0}); 
 
     }, 0); 
 

 
    }); 
 
    }); 
 
</script> 
 

 
<div class="wrap"> 
 
    <img src="http://placehold.it/350x150"/> 
 
    <div id="editable" class="item" contenteditable="true">Write HERE to get very long text and see scroll</div> 
 
</div>