我有一个html5画布。我需要在div(Div1
)中显示它的固定部分。当我在Div1
内滑动时,我需要滚动画布。所以当我滚动时,我会看到画布的相应部分。
我试过这样的事情:
<div id="Div1" style=" float: left; width: 50px; overflow:hidden; ">
<canvas id="myCanvas1" width="200px" style="border: 1px solid #ff0000; position: absolute;">
</canvas>
</div>
答案 0 :(得分:11)
它不起作用(在某些“设计”条件下从div内部滚动画布),首先隐藏溢出。请尝试在画布内滚动内容。
或者,试试这个:http://jsfiddle.net/9g3GG/2/
<div id="Div1" style=" float: left; width: 150px; overflow:scroll; ">
<canvas id="myCanvas1" width="200" style="border:1px solid #ff0000;">asdf asd as asfqwe asd asdfasdf asd as asfqwe asd asdfasdf asd as asfqwe asd asdf</canvas>
</div>
答案 1 :(得分:4)
以下是使用超大画布的演示,并通过调整CSS边距滚动鼠标移动:https://jsfiddle.net/ax7n8944/
HTML:
<div id="canvasdiv" style="width: 500px; height: 250px; overflow: hidden">
<canvas id="canvas" width="10000px" height="250px"></canvas>
</div>
JS:
var canvas = document.getElementById("canvas");
var context = canvas.getContext('2d');
var dragging = false;
var lastX;
var marginLeft = 0;
for (var i = 0; i < 1000; i++) {
context.beginPath();
context.arc(Math.random() * 10000, Math.random() * 250, 20.0, 0, 2 * Math.PI, false);
context.stroke();
}
canvas.addEventListener('mousedown', function(e) {
var evt = e || event;
dragging = true;
lastX = evt.clientX;
e.preventDefault();
}, false);
window.addEventListener('mousemove', function(e) {
var evt = e || event;
if (dragging) {
var delta = evt.clientX - lastX;
lastX = evt.clientX;
marginLeft += delta;
canvas.style.marginLeft = marginLeft + "px";
}
e.preventDefault();
}, false);
window.addEventListener('mouseup', function() {
dragging = false;
}, false);
答案 2 :(得分:2)
最好在画布内滚动,看看这个Phaser插件来做https://jdnichollsc.github.io/Phaser-Kinetic-Scrolling-Plugin/
答案 3 :(得分:0)
希望它能帮到你
答案 4 :(得分:0)
绘制div的行。将行大小设置为0:
DIVofCanvas {
line-height: 0px;
}