【发布时间】:2018-10-30 01:57:10
【问题描述】:
我有一张这样的图片(demo),可以通过鼠标滚轮向上或向下缩放。但它只是可以从左上角缩放...
如何尝试使其从鼠标光标点缩放(例如 example)。 另外,我需要有图片边界,而不是像这个例子是无限的。感谢您的任何回答或建议。
HTML、CSS
//Html
<div id="picture"></div>
//css
#picture {
width: 340px;
height: 150px;
overflow: auto;
}
JavaScript
createSVG = function () {
var svg = d3.select('#picture')
.append('svg')
.attr('width', rw)
.attr('height', rh)
.style('border', '1px solid #000');
svg.append('svg:image')
.attr('xlink:href', 'http://www.iconpng.com/png/beautiful_flat_color/computer.png')
.attr('width', rw)
.attr('height', rh);
}
//Init (Original size)
var Picture_w = 340;
var Picture_h = 150;
value = 96; //(scaling ratio)
rw = Picture_w * value / 100;
rh = Picture_h * value / 100;
createSVG();
// When change scaling ratio, RE createSVG();
changeSize = function () {
d3.select('svg').remove();
rw = Picture_w * value / 100;
rh = Picture_h * value / 100;
createSVG();
};
//mousewheel (Chrome or IE)
$('#picture').on('mousewheel DOMMouseScroll', function (e) {
e.preventDefault();
if (e.type == 'mousewheel') {
if (e.originalEvent.wheelDelta > 0) {
value += 2;
if (value > 500) {
value = 500;
}
changeSize();
// console.log(e.originalEvent.wheelDelta);
}
else {
value -= 2;
if (value < 5) {
value = 5;
}
changeSize();
// console.log("w" + e.originalEvent.wheelDelta);
}
}
});
【问题讨论】:
-
在 createSVG 函数中添加类似的事件处理程序
-
.on("mouseover", changeSize)
标签: javascript html d3.js