自动换行:分解不起作用。可拖动元素中的文本溢出问题
Word-wrap: break-down not working. Text overflow issue in draggable element
请帮忙解决这个问题。目前我有一个 div,它是可拖动的,里面的文本是可编辑的。在这里,用户可以使用输入类型范围更改文本大小。
是否可以隐藏文字-canvas的字母,让文字超出图片的边界-canvas div到达col-sm-8
?
这里当用户写的文本没有 space 时,那么这个词就超出了 col-sm-8
。如何解决这个问题?我使用 overflow:hidden
和 word-wrap:breakdown
,但它不起作用。
function submit_button() {
/* ....Image upload function.. */
}
$(".text-canvas").draggable({
containment: ".imageupload",
create: function() {
$("#text-canvas ").css("width ", 'auto');
},
drag: function() {
$("#text-canvas ").css("width ", 'auto');
},
start: function() {
$("#text-canvas ").css("width ", 'auto');
},
stop: function() {
$("#text-canvas ").css("width ", 'auto');
}
});
$("#fontsize").on("change", function() {
var v = $(this).val();
$('.text-canvas').css('font-size', v + 'px');
});
.text-canvas {
z-index: 1;
position: absolute;
}
.imageupload {
z-index: -1;
}
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="col-sm-4">
<div name="anotherdiv">
<input type="range" min="12" max="54" id="fontsize">
</div>
</div>
<div class="col-sm-8">
<div class="parent-canvas">
<div class="text-canvas" id="text-canvas" contenteditable="true">
my text
</div>
<div class="image-canvas">
<div class="imageupload" onclick="submit_button()">
<img src="img.png">
</div>
</div>
</div>
</div>
然后我用了.text-canvas{ word-break: break-all; }
。现在文本不会超出 col-sm-8
但它仍然会超出图像 - div.
UPDATE :currently I solved the issue by using padding-right:10%
. But I
don't think is a good method. Please suggest a correct method.
你需要https://developer.mozilla.org/en-US/docs/Web/CSS/word-break
word-break: break-all;
function submit_button() {
/* ....Image upload function.. */
}
$(".text-canvas").draggable({
containment: ".imageupload",
create: function() {
$("#text-canvas ").css("width ", 'auto');
},
drag: function() {
$("#text-canvas ").css("width ", 'auto');
},
start: function() {
$("#text-canvas ").css("width ", 'auto');
},
stop: function() {
$("#text-canvas ").css("width ", 'auto');
}
});
$("#fontsize").on("change", function() {
var v = $(this).val();
$('.text-canvas').css('font-size', v + 'px');
});
.text-canvas {
z-index: 1;
position: absolute;
word-break: break-all;
}
.imageupload {
z-index: -1;
}
.parent-canvas {
position: relative;
}
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="col-sm-4">
<div name="anotherdiv">
<input type="range" min="12" max="54" id="fontsize">
</div>
</div>
<div class="col-sm-8">
<div class="parent-canvas">
<div class="text-canvas" id="text-canvas" contenteditable="true">
my text
</div>
<div class="image-canvas">
<div class="imageupload" onclick="submit_button()">
<img src="img.png">
</div>
</div>
</div>
</div>
使用text-overflow: ellipsis;
或text-overflow: clip;
它会隐藏溢出文本。
function submit_button() {
/* ....Image upload function.. */
}
$(".text-canvas").draggable({
containment: ".imageupload",
create: function() {
$("#text-canvas ").css("width ", 'auto');
},
drag: function() {
$("#text-canvas ").css("width ", 'auto');
},
start: function() {
$("#text-canvas ").css("width ", 'auto');
},
stop: function() {
$("#text-canvas ").css("width ", 'auto');
}
});
$("#fontsize").on("change", function() {
var v = $(this).val();
$('.text-canvas').css('font-size', v + 'px');
});
.text-canvas {
z-index: 1;
position: absolute;
text-overflow: clip;/*ellipsis,initial*/
white-space: nowrap;
overflow: hidden;
width:3em;/*image div width*/
}
.imageupload {
z-index: -1;
}
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="col-sm-4">
<div name="anotherdiv">
<input type="range" min="12" max="54" id="fontsize">
</div>
</div>
<div class="col-sm-8">
<div class="parent-canvas">
<div class="text-canvas" id="text-canvas" contenteditable="true">
my text
</div>
<div class="image-canvas">
<div class="imageupload" onclick="submit_button()">
<img src="img.png">
</div>
</div>
</div>
</div>
将 .parent-canvas
的尺寸设置为与图像相同,然后使用 overflow: hidden
和 position-relative
包含绝对定位的文本。
.parent-canvas {
width: 300px;
height: 200px;
overflow: hidden;
position: relative;
}
function submit_button() {
/* ....Image upload function.. */
}
$(".text-canvas").draggable({
containment: ".imageupload",
create: function() {
$("#text-canvas ").css("width ", 'auto');
},
drag: function() {
$("#text-canvas ").css("width ", 'auto');
},
start: function() {
$("#text-canvas ").css("width ", 'auto');
},
stop: function() {
$("#text-canvas ").css("width ", 'auto');
}
});
$("#fontsize").on("change", function() {
var v = $(this).val();
$('.text-canvas').css('font-size', v + 'px');
});
.parent-canvas {
width: 300px;
height: 200px;
overflow: hidden;
position: relative;
}
.text-canvas {
z-index: 1;
position: absolute;
}
.imageupload {
z-index: -1;
}
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="col-sm-4">
<div name="anotherdiv">
<input type="range" min="12" max="54" id="fontsize">
</div>
</div>
<div class="col-sm-8">
<div class="parent-canvas">
<div class="text-canvas" id="text-canvas" contenteditable="true">
my text
</div>
<div class="image-canvas">
<div class="imageupload" onclick="submit_button()">
<img src="http://placehold.it/300x200">
</div>
</div>
</div>
</div>
解决方案:
您需要在 .parent-canvas
class 中使用以下属性:
.parent-canvas {
display: inline-block; /* Display inline but retain the block-level characteristics */
overflow: hidden; /* Hide the text if it overflows the container */
position: relative; /* Text is removed from the normal flow with absolute position, use this to contain it */
}
之后你有两个选择,在你的 .text-canvas
class:
中使用 word-break
or max-width
.text-canvas {
word-break: break-all;
}
代码段:
function submit_button() {
/* ....Image upload function.. */
}
$(".text-canvas").draggable({
containment: ".imageupload",
create: function() {
$("#text-canvas ").css("width ", 'auto');
},
drag: function() {
$("#text-canvas ").css("width ", 'auto');
},
start: function() {
$("#text-canvas ").css("width ", 'auto');
},
stop: function() {
$("#text-canvas ").css("width ", 'auto');
}
});
$("#fontsize").on("change", function() {
var v = $(this).val();
$('.text-canvas').css('font-size', v + 'px');
});
.text-canvas {
z-index: 1;
position: absolute;
}
.imageupload {
z-index: -1;
}
.parent-canvas {
display: inline-block;
overflow: hidden;
position: relative;
}
.text-canvas {
word-break: break-all;
}
.image-canvas img {
vertical-align: middle;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="col-sm-4">
<div name="anotherdiv">
<input type="range" min="12" max="54" id="fontsize">
</div>
</div>
<div class="col-sm-8">
<div class="parent-canvas">
<div class="text-canvas" id="text-canvas" contenteditable="true">
my text
</div>
<div class="image-canvas">
<div class="imageupload" onclick="submit_button()">
<img src="http://placehold.it/100x100">
</div>
</div>
</div>
</div>
或
.text-canvas {
max-width: 100%;
}
代码段:
function submit_button() {
/* ....Image upload function.. */
}
$(".text-canvas").draggable({
containment: ".imageupload",
create: function() {
$("#text-canvas ").css("width ", 'auto');
},
drag: function() {
$("#text-canvas ").css("width ", 'auto');
},
start: function() {
$("#text-canvas ").css("width ", 'auto');
},
stop: function() {
$("#text-canvas ").css("width ", 'auto');
}
});
$("#fontsize").on("change", function() {
var v = $(this).val();
$('.text-canvas').css('font-size', v + 'px');
});
.text-canvas {
z-index: 1;
position: absolute;
}
.imageupload {
z-index: -1;
}
.parent-canvas {
display: inline-block;
overflow: hidden;
position: relative;
}
.text-canvas {
max-width: 100%;
}
.image-canvas img {
vertical-align: middle;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="col-sm-4">
<div name="anotherdiv">
<input type="range" min="12" max="54" id="fontsize">
</div>
</div>
<div class="col-sm-8">
<div class="parent-canvas">
<div class="text-canvas" id="text-canvas" contenteditable="true">
my text
</div>
<div class="image-canvas">
<div class="imageupload" onclick="submit_button()">
<img src="http://placehold.it/100x100">
</div>
</div>
</div>
</div>
如果你喜欢也可以同时使用,它们不会相互干扰:
.text-canvas {
word-break: break-all;
max-width: 100%;
}
代码片段:
function submit_button() {
/* ....Image upload function.. */
}
$(".text-canvas").draggable({
containment: ".imageupload",
create: function() {
$("#text-canvas ").css("width ", 'auto');
},
drag: function() {
$("#text-canvas ").css("width ", 'auto');
},
start: function() {
$("#text-canvas ").css("width ", 'auto');
},
stop: function() {
$("#text-canvas ").css("width ", 'auto');
}
});
$("#fontsize").on("change", function() {
var v = $(this).val();
$('.text-canvas').css('font-size', v + 'px');
});
.text-canvas {
z-index: 1;
position: absolute;
}
.imageupload {
z-index: -1;
}
.parent-canvas {
display: inline-block;
overflow: hidden;
position: relative;
}
.text-canvas {
word-break: break-all;
max-width: 100%;
}
.image-canvas img {
vertical-align: middle;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="col-sm-4">
<div name="anotherdiv">
<input type="range" min="12" max="54" id="fontsize">
</div>
</div>
<div class="col-sm-8">
<div class="parent-canvas">
<div class="text-canvas" id="text-canvas" contenteditable="true">
ttttttttttttttttttttttttttttttttttttttt
</div>
<div class="image-canvas">
<div class="imageupload" onclick="submit_button()">
<img src="http://placehold.it/100x100">
</div>
</div>
</div>
</div>
jsFiddle
测试:
Windows 10(64 位):
- Chrome 54.0.2840.71 米
- 火狐 49.0.2
- 边 25.10586.0.0
IOS 10.1:
- Iphone 6 移动浏览器
可能的用户体验改进:
在我看来,可以通过添加 属性 cursor: grab;
和 cursor: grabbing;
来稍微改善用户体验,至少对于 Chrome 和 FF用户知道它是一个可拖动的元素。
我发布这个答案后不知道它是一个可拖动的项目。我想我没有真正关注整个问题...人们倾向于快速与事物互动。
您需要决定 cursor: text;
是否比 grab
和 grabbing
更好。
添加到 jQuery 可拖动的属性 classes:
.ui-draggable {
cursor: -webkit-grab;
cursor: grab;
}
.ui-draggable-dragging {
cursor: -webkit-grabbing;
cursor: grabbing;
}
代码段:
function submit_button() {
/* ....Image upload function.. */
}
$(".text-canvas").draggable({
containment: ".imageupload",
create: function() {
$("#text-canvas ").css("width ", 'auto');
},
drag: function() {
$("#text-canvas ").css("width ", 'auto');
},
start: function() {
$("#text-canvas ").css("width ", 'auto');
},
stop: function() {
$("#text-canvas ").css("width ", 'auto');
}
});
$("#fontsize").on("change", function() {
var v = $(this).val();
$('.text-canvas').css('font-size', v + 'px');
});
.text-canvas {
z-index: 1;
position: absolute;
}
.imageupload {
z-index: -1;
}
.parent-canvas {
display: inline-block;
overflow: hidden;
position: relative;
}
.text-canvas {
word-break: break-all;
max-width: 100%;
}
.image-canvas img {
vertical-align: middle;
}
.ui-draggable {
cursor: -webkit-grab;
cursor: grab;
}
.ui-draggable-dragging {
cursor: -webkit-grabbing;
cursor: grabbing;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="col-sm-4">
<div name="anotherdiv">
<input type="range" min="12" max="54" id="fontsize">
</div>
</div>
<div class="col-sm-8">
<div class="parent-canvas">
<div class="text-canvas" id="text-canvas" contenteditable="true">
my text
</div>
<div class="image-canvas">
<div class="imageupload" onclick="submit_button()">
<img src="http://placehold.it/100x100">
</div>
</div>
</div>
</div>
备注:
- 要删除
img
元素下不需要的 space,请使用 属性 vertical-align: middle;
.
编辑:
为了防止font-size
增大时文字垂直溢出,需要将height: auto;
设置为.text-canvas
class.
你可以用不同的方式来做到这一点,一个可能是这样做的:
$("#fontsize").on("change", function() {
var v = $(this).val();
$('.text-canvas').css({
'font-size': v + 'px',
'height': 'auto'
});
});
这将设置 auto
值覆盖 jQuery UI 设置的值。
In the demo, the image is quite small and the font-size can get too
big, making its container dimensions exceed its parent's, make sure to
control that in production.
代码段:
function submit_button() {
/* ....Image upload function.. */
}
$(".text-canvas").draggable({
containment: ".imageupload",
create: function() {
$("#text-canvas ").css("width ", 'auto');
},
drag: function() {
$("#text-canvas ").css("width ", 'auto');
},
start: function() {
$("#text-canvas ").css("width ", 'auto');
},
stop: function() {
$("#text-canvas ").css("width ", 'auto');
}
});
$("#fontsize").on("change", function() {
var v = $(this).val();
$('.text-canvas').css({
'font-size': v + 'px',
'height': 'auto'
});
});
.text-canvas {
z-index: 1;
position: absolute;
}
.imageupload {
z-index: -1;
}
.parent-canvas {
display: inline-block;
overflow: hidden;
position: relative;
}
.text-canvas {
word-break: break-all;
max-width: 100%;
}
.image-canvas img {
vertical-align: middle;
}
.ui-draggable {
cursor: -webkit-grab;
cursor: grab;
}
.ui-draggable-dragging {
cursor: -webkit-grabbing;
cursor: grabbing;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="col-sm-4">
<div name="anotherdiv">
<input type="range" min="12" max="54" id="fontsize">
</div>
</div>
<div class="col-sm-8">
<div class="parent-canvas">
<div class="text-canvas" id="text-canvas" contenteditable="true">
my text
</div>
<div class="image-canvas">
<div class="imageupload" onclick="submit_button()">
<img src="http://placehold.it/100x100">
</div>
</div>
</div>
</div>
对于parent
.parent-canvas{
display: inline-block;
overflow: hidden;
position: relative;
}
对于Child (text-canvas)
.text-canvas {
word-break: break-all;
}
演示
-为更好的演示目的添加了背景颜色
-立即更改字体大小
function submit_button() {
/* ....Image upload function.. */
}
$(".text-canvas").draggable({
containment: ".imageupload",
create: function() {
$("#text-canvas ").css("width ", 'auto');
},
drag: function() {
$("#text-canvas ").css("width ", 'auto');
},
start: function() {
$("#text-canvas ").css("width ", 'auto');
},
stop: function() {
$("#text-canvas ").css("width ", 'auto');
}
});
$("#fontsize").on("input change", function() {
var v = $(this).val();
$('.text-canvas').css('font-size', v + 'px');
});
.text-canvas {
z-index: 1;
position: absolute;
word-break: break-all;
}
.imageupload {
z-index: -1;
}
.col-sm-6 {
background: red;
}
.c {padding: 5px;}
.parent-canvas{
background: orange;
display: inline-block;
overflow: hidden;
position: relative;
}
.text-canvas {
background: yellow;
min-width: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery-ui-bootstrap/0.5pre/assets/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="c col-sm-2">
<div name="anotherdiv" style="display:flex">
<div>8px</div>
<input type="range" min="8" max="30" id="fontsize">
<div>30px</div>
</div>
</div>
<div class="c col-sm-12"></div>
<div class="c col-sm-6">
<div class="parent-canvas">
<div class="text-canvas" id="text-canvas" contenteditable="true">
my text
</div>
<div class="image-canvas">
<div class="imageupload" onclick="submit_button()">
<img src="http://lorempixel.com/150/150/">
</div>
</div>
</div>
</div>
请帮忙解决这个问题。目前我有一个 div,它是可拖动的,里面的文本是可编辑的。在这里,用户可以使用输入类型范围更改文本大小。
是否可以隐藏文字-canvas的字母,让文字超出图片的边界-canvas div到达col-sm-8
?
这里当用户写的文本没有 space 时,那么这个词就超出了 col-sm-8
。如何解决这个问题?我使用 overflow:hidden
和 word-wrap:breakdown
,但它不起作用。
function submit_button() {
/* ....Image upload function.. */
}
$(".text-canvas").draggable({
containment: ".imageupload",
create: function() {
$("#text-canvas ").css("width ", 'auto');
},
drag: function() {
$("#text-canvas ").css("width ", 'auto');
},
start: function() {
$("#text-canvas ").css("width ", 'auto');
},
stop: function() {
$("#text-canvas ").css("width ", 'auto');
}
});
$("#fontsize").on("change", function() {
var v = $(this).val();
$('.text-canvas').css('font-size', v + 'px');
});
.text-canvas {
z-index: 1;
position: absolute;
}
.imageupload {
z-index: -1;
}
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="col-sm-4">
<div name="anotherdiv">
<input type="range" min="12" max="54" id="fontsize">
</div>
</div>
<div class="col-sm-8">
<div class="parent-canvas">
<div class="text-canvas" id="text-canvas" contenteditable="true">
my text
</div>
<div class="image-canvas">
<div class="imageupload" onclick="submit_button()">
<img src="img.png">
</div>
</div>
</div>
</div>
然后我用了.text-canvas{ word-break: break-all; }
。现在文本不会超出 col-sm-8
但它仍然会超出图像 - div.
UPDATE :currently I solved the issue by using
padding-right:10%
. But I don't think is a good method. Please suggest a correct method.
你需要https://developer.mozilla.org/en-US/docs/Web/CSS/word-break
word-break: break-all;
function submit_button() {
/* ....Image upload function.. */
}
$(".text-canvas").draggable({
containment: ".imageupload",
create: function() {
$("#text-canvas ").css("width ", 'auto');
},
drag: function() {
$("#text-canvas ").css("width ", 'auto');
},
start: function() {
$("#text-canvas ").css("width ", 'auto');
},
stop: function() {
$("#text-canvas ").css("width ", 'auto');
}
});
$("#fontsize").on("change", function() {
var v = $(this).val();
$('.text-canvas').css('font-size', v + 'px');
});
.text-canvas {
z-index: 1;
position: absolute;
word-break: break-all;
}
.imageupload {
z-index: -1;
}
.parent-canvas {
position: relative;
}
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="col-sm-4">
<div name="anotherdiv">
<input type="range" min="12" max="54" id="fontsize">
</div>
</div>
<div class="col-sm-8">
<div class="parent-canvas">
<div class="text-canvas" id="text-canvas" contenteditable="true">
my text
</div>
<div class="image-canvas">
<div class="imageupload" onclick="submit_button()">
<img src="img.png">
</div>
</div>
</div>
</div>
使用text-overflow: ellipsis;
或text-overflow: clip;
它会隐藏溢出文本。
function submit_button() {
/* ....Image upload function.. */
}
$(".text-canvas").draggable({
containment: ".imageupload",
create: function() {
$("#text-canvas ").css("width ", 'auto');
},
drag: function() {
$("#text-canvas ").css("width ", 'auto');
},
start: function() {
$("#text-canvas ").css("width ", 'auto');
},
stop: function() {
$("#text-canvas ").css("width ", 'auto');
}
});
$("#fontsize").on("change", function() {
var v = $(this).val();
$('.text-canvas').css('font-size', v + 'px');
});
.text-canvas {
z-index: 1;
position: absolute;
text-overflow: clip;/*ellipsis,initial*/
white-space: nowrap;
overflow: hidden;
width:3em;/*image div width*/
}
.imageupload {
z-index: -1;
}
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="col-sm-4">
<div name="anotherdiv">
<input type="range" min="12" max="54" id="fontsize">
</div>
</div>
<div class="col-sm-8">
<div class="parent-canvas">
<div class="text-canvas" id="text-canvas" contenteditable="true">
my text
</div>
<div class="image-canvas">
<div class="imageupload" onclick="submit_button()">
<img src="img.png">
</div>
</div>
</div>
</div>
将 .parent-canvas
的尺寸设置为与图像相同,然后使用 overflow: hidden
和 position-relative
包含绝对定位的文本。
.parent-canvas {
width: 300px;
height: 200px;
overflow: hidden;
position: relative;
}
function submit_button() {
/* ....Image upload function.. */
}
$(".text-canvas").draggable({
containment: ".imageupload",
create: function() {
$("#text-canvas ").css("width ", 'auto');
},
drag: function() {
$("#text-canvas ").css("width ", 'auto');
},
start: function() {
$("#text-canvas ").css("width ", 'auto');
},
stop: function() {
$("#text-canvas ").css("width ", 'auto');
}
});
$("#fontsize").on("change", function() {
var v = $(this).val();
$('.text-canvas').css('font-size', v + 'px');
});
.parent-canvas {
width: 300px;
height: 200px;
overflow: hidden;
position: relative;
}
.text-canvas {
z-index: 1;
position: absolute;
}
.imageupload {
z-index: -1;
}
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="col-sm-4">
<div name="anotherdiv">
<input type="range" min="12" max="54" id="fontsize">
</div>
</div>
<div class="col-sm-8">
<div class="parent-canvas">
<div class="text-canvas" id="text-canvas" contenteditable="true">
my text
</div>
<div class="image-canvas">
<div class="imageupload" onclick="submit_button()">
<img src="http://placehold.it/300x200">
</div>
</div>
</div>
</div>
解决方案:
您需要在 .parent-canvas
class 中使用以下属性:
.parent-canvas {
display: inline-block; /* Display inline but retain the block-level characteristics */
overflow: hidden; /* Hide the text if it overflows the container */
position: relative; /* Text is removed from the normal flow with absolute position, use this to contain it */
}
之后你有两个选择,在你的 .text-canvas
class:
word-break
or max-width
.text-canvas {
word-break: break-all;
}
代码段:
function submit_button() {
/* ....Image upload function.. */
}
$(".text-canvas").draggable({
containment: ".imageupload",
create: function() {
$("#text-canvas ").css("width ", 'auto');
},
drag: function() {
$("#text-canvas ").css("width ", 'auto');
},
start: function() {
$("#text-canvas ").css("width ", 'auto');
},
stop: function() {
$("#text-canvas ").css("width ", 'auto');
}
});
$("#fontsize").on("change", function() {
var v = $(this).val();
$('.text-canvas').css('font-size', v + 'px');
});
.text-canvas {
z-index: 1;
position: absolute;
}
.imageupload {
z-index: -1;
}
.parent-canvas {
display: inline-block;
overflow: hidden;
position: relative;
}
.text-canvas {
word-break: break-all;
}
.image-canvas img {
vertical-align: middle;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="col-sm-4">
<div name="anotherdiv">
<input type="range" min="12" max="54" id="fontsize">
</div>
</div>
<div class="col-sm-8">
<div class="parent-canvas">
<div class="text-canvas" id="text-canvas" contenteditable="true">
my text
</div>
<div class="image-canvas">
<div class="imageupload" onclick="submit_button()">
<img src="http://placehold.it/100x100">
</div>
</div>
</div>
</div>
或
.text-canvas {
max-width: 100%;
}
代码段:
function submit_button() {
/* ....Image upload function.. */
}
$(".text-canvas").draggable({
containment: ".imageupload",
create: function() {
$("#text-canvas ").css("width ", 'auto');
},
drag: function() {
$("#text-canvas ").css("width ", 'auto');
},
start: function() {
$("#text-canvas ").css("width ", 'auto');
},
stop: function() {
$("#text-canvas ").css("width ", 'auto');
}
});
$("#fontsize").on("change", function() {
var v = $(this).val();
$('.text-canvas').css('font-size', v + 'px');
});
.text-canvas {
z-index: 1;
position: absolute;
}
.imageupload {
z-index: -1;
}
.parent-canvas {
display: inline-block;
overflow: hidden;
position: relative;
}
.text-canvas {
max-width: 100%;
}
.image-canvas img {
vertical-align: middle;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="col-sm-4">
<div name="anotherdiv">
<input type="range" min="12" max="54" id="fontsize">
</div>
</div>
<div class="col-sm-8">
<div class="parent-canvas">
<div class="text-canvas" id="text-canvas" contenteditable="true">
my text
</div>
<div class="image-canvas">
<div class="imageupload" onclick="submit_button()">
<img src="http://placehold.it/100x100">
</div>
</div>
</div>
</div>
如果你喜欢也可以同时使用,它们不会相互干扰:
.text-canvas {
word-break: break-all;
max-width: 100%;
}
代码片段:
function submit_button() {
/* ....Image upload function.. */
}
$(".text-canvas").draggable({
containment: ".imageupload",
create: function() {
$("#text-canvas ").css("width ", 'auto');
},
drag: function() {
$("#text-canvas ").css("width ", 'auto');
},
start: function() {
$("#text-canvas ").css("width ", 'auto');
},
stop: function() {
$("#text-canvas ").css("width ", 'auto');
}
});
$("#fontsize").on("change", function() {
var v = $(this).val();
$('.text-canvas').css('font-size', v + 'px');
});
.text-canvas {
z-index: 1;
position: absolute;
}
.imageupload {
z-index: -1;
}
.parent-canvas {
display: inline-block;
overflow: hidden;
position: relative;
}
.text-canvas {
word-break: break-all;
max-width: 100%;
}
.image-canvas img {
vertical-align: middle;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="col-sm-4">
<div name="anotherdiv">
<input type="range" min="12" max="54" id="fontsize">
</div>
</div>
<div class="col-sm-8">
<div class="parent-canvas">
<div class="text-canvas" id="text-canvas" contenteditable="true">
ttttttttttttttttttttttttttttttttttttttt
</div>
<div class="image-canvas">
<div class="imageupload" onclick="submit_button()">
<img src="http://placehold.it/100x100">
</div>
</div>
</div>
</div>
jsFiddle
测试:
Windows 10(64 位):
- Chrome 54.0.2840.71 米
- 火狐 49.0.2
- 边 25.10586.0.0
IOS 10.1:
- Iphone 6 移动浏览器
可能的用户体验改进:
在我看来,可以通过添加 属性 cursor: grab;
和 cursor: grabbing;
来稍微改善用户体验,至少对于 Chrome 和 FF用户知道它是一个可拖动的元素。
我发布这个答案后不知道它是一个可拖动的项目。我想我没有真正关注整个问题...人们倾向于快速与事物互动。
您需要决定 cursor: text;
是否比 grab
和 grabbing
更好。
添加到 jQuery 可拖动的属性 classes:
.ui-draggable {
cursor: -webkit-grab;
cursor: grab;
}
.ui-draggable-dragging {
cursor: -webkit-grabbing;
cursor: grabbing;
}
代码段:
function submit_button() {
/* ....Image upload function.. */
}
$(".text-canvas").draggable({
containment: ".imageupload",
create: function() {
$("#text-canvas ").css("width ", 'auto');
},
drag: function() {
$("#text-canvas ").css("width ", 'auto');
},
start: function() {
$("#text-canvas ").css("width ", 'auto');
},
stop: function() {
$("#text-canvas ").css("width ", 'auto');
}
});
$("#fontsize").on("change", function() {
var v = $(this).val();
$('.text-canvas').css('font-size', v + 'px');
});
.text-canvas {
z-index: 1;
position: absolute;
}
.imageupload {
z-index: -1;
}
.parent-canvas {
display: inline-block;
overflow: hidden;
position: relative;
}
.text-canvas {
word-break: break-all;
max-width: 100%;
}
.image-canvas img {
vertical-align: middle;
}
.ui-draggable {
cursor: -webkit-grab;
cursor: grab;
}
.ui-draggable-dragging {
cursor: -webkit-grabbing;
cursor: grabbing;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="col-sm-4">
<div name="anotherdiv">
<input type="range" min="12" max="54" id="fontsize">
</div>
</div>
<div class="col-sm-8">
<div class="parent-canvas">
<div class="text-canvas" id="text-canvas" contenteditable="true">
my text
</div>
<div class="image-canvas">
<div class="imageupload" onclick="submit_button()">
<img src="http://placehold.it/100x100">
</div>
</div>
</div>
</div>
备注:
- 要删除
img
元素下不需要的 space,请使用 属性vertical-align: middle;
.
编辑:
为了防止font-size
增大时文字垂直溢出,需要将height: auto;
设置为.text-canvas
class.
你可以用不同的方式来做到这一点,一个可能是这样做的:
$("#fontsize").on("change", function() {
var v = $(this).val();
$('.text-canvas').css({
'font-size': v + 'px',
'height': 'auto'
});
});
这将设置 auto
值覆盖 jQuery UI 设置的值。
In the demo, the image is quite small and the font-size can get too big, making its container dimensions exceed its parent's, make sure to control that in production.
代码段:
function submit_button() {
/* ....Image upload function.. */
}
$(".text-canvas").draggable({
containment: ".imageupload",
create: function() {
$("#text-canvas ").css("width ", 'auto');
},
drag: function() {
$("#text-canvas ").css("width ", 'auto');
},
start: function() {
$("#text-canvas ").css("width ", 'auto');
},
stop: function() {
$("#text-canvas ").css("width ", 'auto');
}
});
$("#fontsize").on("change", function() {
var v = $(this).val();
$('.text-canvas').css({
'font-size': v + 'px',
'height': 'auto'
});
});
.text-canvas {
z-index: 1;
position: absolute;
}
.imageupload {
z-index: -1;
}
.parent-canvas {
display: inline-block;
overflow: hidden;
position: relative;
}
.text-canvas {
word-break: break-all;
max-width: 100%;
}
.image-canvas img {
vertical-align: middle;
}
.ui-draggable {
cursor: -webkit-grab;
cursor: grab;
}
.ui-draggable-dragging {
cursor: -webkit-grabbing;
cursor: grabbing;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div class="col-sm-4">
<div name="anotherdiv">
<input type="range" min="12" max="54" id="fontsize">
</div>
</div>
<div class="col-sm-8">
<div class="parent-canvas">
<div class="text-canvas" id="text-canvas" contenteditable="true">
my text
</div>
<div class="image-canvas">
<div class="imageupload" onclick="submit_button()">
<img src="http://placehold.it/100x100">
</div>
</div>
</div>
</div>
对于parent
.parent-canvas{
display: inline-block;
overflow: hidden;
position: relative;
}
对于Child (text-canvas)
.text-canvas {
word-break: break-all;
}
演示
-为更好的演示目的添加了背景颜色
-立即更改字体大小
function submit_button() {
/* ....Image upload function.. */
}
$(".text-canvas").draggable({
containment: ".imageupload",
create: function() {
$("#text-canvas ").css("width ", 'auto');
},
drag: function() {
$("#text-canvas ").css("width ", 'auto');
},
start: function() {
$("#text-canvas ").css("width ", 'auto');
},
stop: function() {
$("#text-canvas ").css("width ", 'auto');
}
});
$("#fontsize").on("input change", function() {
var v = $(this).val();
$('.text-canvas').css('font-size', v + 'px');
});
.text-canvas {
z-index: 1;
position: absolute;
word-break: break-all;
}
.imageupload {
z-index: -1;
}
.col-sm-6 {
background: red;
}
.c {padding: 5px;}
.parent-canvas{
background: orange;
display: inline-block;
overflow: hidden;
position: relative;
}
.text-canvas {
background: yellow;
min-width: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery-ui-bootstrap/0.5pre/assets/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="c col-sm-2">
<div name="anotherdiv" style="display:flex">
<div>8px</div>
<input type="range" min="8" max="30" id="fontsize">
<div>30px</div>
</div>
</div>
<div class="c col-sm-12"></div>
<div class="c col-sm-6">
<div class="parent-canvas">
<div class="text-canvas" id="text-canvas" contenteditable="true">
my text
</div>
<div class="image-canvas">
<div class="imageupload" onclick="submit_button()">
<img src="http://lorempixel.com/150/150/">
</div>
</div>
</div>
</div>