使用javascript的图像缩小功能
我正在做一个项目,其中有一个图像时,用户点击图像放大和再次点击图像缩小。如何在代码中创建缩小功能使用javascript的图像缩小功能,javascript,jquery,Javascript,Jquery,我正在做一个项目,其中有一个图像时,用户点击图像放大和再次点击图像缩小。如何在代码中创建缩小功能 这里不需要很多javascript。只需要一些好的CSS,如下所示: JS: css: 试试看下面的代码是否对您有帮助 编辑:以防您希望在同一页面中的多个图像上执行此操作 var zoom = "in"; $('.image').on('click', function () { this.zoom = (this.zoom == "in") ? "out" : "in"; con
这里不需要很多javascript。只需要一些好的CSS,如下所示: JS: css:
试试看下面的代码是否对您有帮助 编辑:以防您希望在同一页面中的多个图像上执行此操作
var zoom = "in";
$('.image').on('click', function () {
this.zoom = (this.zoom == "in") ? "out" : "in";
console.log(this.zoom);
resizeImg(this);
});
function resizeImg (img) {
var scale = 150/100;
var pos = $(img).offset();
var clickX = event.pageX - pos.left;
var clickY = event.pageY - pos.top;
var container = $(img).parent().get(0);
if(img.zoom == "in"){
$(img).css({
width: img.width*scale,
height: img.height*scale
});
}
else if (img.zoom == "out"){
$(img).css({
width: img.width/scale,
height: img.height/scale
});
}
container.scrollLeft = ($(container).width() / -2 ) + clickX * scale;
container.scrollTop = ($(container).height() / -2 ) + clickY * scale;
}
为所有图像设置zoom
attribute=0-->$('img').attr('zoom',0)代码>
如果zoom
属性设置为0
我们将使zoom
生效并设置属性zoom=1
否则zoom
属性设置为已缩放的1
,我们设置属性zoom=0
,并删除style
属性以使图像恢复正常。如果您有多个图像,这也可能对您有所帮助
JSfiddle:
JS代码:
$('.image').on('click', function () {
if (typeof this.toggleZoom === "undefined") this.toggleZoom = false;
this.toggleZoom = !this.toggleZoom;
resizeImg(this);
});
function resizeImg(img) {
var scale = (img.toggleZoom) ? 150 / 100 : 100 / 150;
console.log(scale);
var pos = $(img).offset();
var clickX = event.pageX - pos.left;
var clickY = event.pageY - pos.top;
var container = $(img).parent().get(0);
$(img).css({
width: img.width * scale,
height: img.height * scale
});
container.scrollLeft = ($(container).width() / -2) + clickX * scale;
container.scrollTop = ($(container).height() / -2) + clickY * scale;
}
我补充的
及
你是说第一次点击应该放大,下一次应该缩小?是的,这正是我想要的。
#Container {
position:relative;
overflow:auto;
}
.zoom{
-webkit-transform : scale(1.5);
transform: scale(1.5);
}
var zoom = "in";
$('.image').on('click', function () {
this.zoom = (this.zoom == "in") ? "out" : "in";
console.log(this.zoom);
resizeImg(this);
});
function resizeImg (img) {
var scale = 150/100;
var pos = $(img).offset();
var clickX = event.pageX - pos.left;
var clickY = event.pageY - pos.top;
var container = $(img).parent().get(0);
if(img.zoom == "in"){
$(img).css({
width: img.width*scale,
height: img.height*scale
});
}
else if (img.zoom == "out"){
$(img).css({
width: img.width/scale,
height: img.height/scale
});
}
container.scrollLeft = ($(container).width() / -2 ) + clickX * scale;
container.scrollTop = ($(container).height() / -2 ) + clickY * scale;
}
$('img').attr('zoom', 0);
$('.image').on('click', function () {
resizeImg(this);
});
function resizeImg(img) {
var zoom = $(img).attr('zoom');
if (zoom == 0) {
var scale = 150 / 100;
var pos = $(img).offset();
var clickX = event.pageX - pos.left;
var clickY = event.pageY - pos.top;
var container = $(img).parent().get(0);
$(img).css({
width: img.width * scale,
height: img.height * scale
}).attr('zoom', 1);
container.scrollLeft = ($(container).width() / -2) + clickX * scale;
container.scrollTop = ($(container).height() / -2) + clickY * scale;
}else{
$(img).attr('zoom',0).attr('style','');
}
}
$('.image').on('click', function () {
if (typeof this.toggleZoom === "undefined") this.toggleZoom = false;
this.toggleZoom = !this.toggleZoom;
resizeImg(this);
});
function resizeImg(img) {
var scale = (img.toggleZoom) ? 150 / 100 : 100 / 150;
console.log(scale);
var pos = $(img).offset();
var clickX = event.pageX - pos.left;
var clickY = event.pageY - pos.top;
var container = $(img).parent().get(0);
$(img).css({
width: img.width * scale,
height: img.height * scale
});
container.scrollLeft = ($(container).width() / -2) + clickX * scale;
container.scrollTop = ($(container).height() / -2) + clickY * scale;
}
if (typeof this.toggleZoom === "undefined") this.toggleZoom = false;
this.toggleZoom = !this.toggleZoom;
var scale = (img.toggleZoom) ? 150 / 100 : 100 / 150;