Home > front end >  Enlarge the transform ratio js, the moving range has expanded
Enlarge the transform ratio js, the moving range has expanded

Time:11-30

Div designated problem is in the high school wide double refers to enlarge cannot exceed boundary, the diagram to drag in the no. 1, 2, can also at a random location for zooming, if in a fixed position is center can use scaling, that at a random location, how to make it scaling is not beyond the border, and that he does not move when the offset with the growing proportion and mobile ran out of the specified border and narrowing the scope of the mobile end


 
$(function () {
$(" # SDH "). The draggable ({
Containment: '# sd,
});
$(" # SDH "). The resizable ({
Handles: 'all',
Containment: '# sd,
});
});

Var eleImg=document. QuerySelector (' # SDH);
Var store={
Scale: 1
};

//zoom event processing
EleImg. AddEventListener (' touchstart ', function (event) {
Var touches=event. Touches;
Var events=touches [0];
Var events2=touches [1].

Event. The preventDefault ();

//the first touch point coordinate
Store. PageX=events. PageX;
Store. PageY=events. PageY;
Store. The moveable=true;

If (events2) {
Store. PageX2=events2. PageX;
Store. PageY2=events2. PageY;
}
Store. OriginScale=store. Scale | | 1;
});
Document. The addEventListener (' touchmove ', function (event) {
if (! Store. The moveable) {
return;
}
Var touches=event. Touches;
Var events=touches [0];
Var events2=touches [1].
//double refers to mobile
If (events2) {
//the second finger coordinates when touchmove for
if (! Store. PageX2) {
Store. PageX2=events2. PageX;
}
if (! Store. PageY2) {
Store. PageY2=events2. PageY;
}

//get the coordinate between example
Var getDistance=function (start, stop) {
Return math.h hypot (stop. X - start. X, stop. The y - start. Y);
};

//double refers to scaling calculation
Var zoom=getDistance ({
X: events. PageX,
Y: events. PageY
}, {
X: events2. PageX,
Y: events2. PageY
})/
GetDistance ({
X: store pageX,
Y: store. PageY
}, {
X: store pageX2,
Y: store. PageY2
});

Var a_x=$(" # body "). Offset () left.
Var a_y=$(" # body "). Offset (). The top;
Var xx=$(" # sd "). Offset () left.
Var yy=$(" # sd "). Offset (). The top;
Var ww=$(" # sd "). OuterWidth () + 1;
Var hh=$(" # sd "). OuterHeight () + 1;

Var ww2=$(eleImg.) width ();
Var hh2=$(eleImg). Height ();
Var xx2=$(eleImg). Offset () left.
Var yy2=$(eleImg). Offset (). The top;

//element for left () up and down, right, up and down) the position of the Angle of
Var upper_right_X=(xx2 - xx) + ww2.//the upper right corner of the X
Var lower_left_Y=(yy2 - yy) + hh2;//left corner Y
Var upper_left_X=(xx2 - xx);//the top left corner X
Var upper_left_Y=(yy2 - yy);//the top left corner Y

//for border of the window (top and bottom left), right (up) the position of the Angle of
Var upper_right_X2=(xx - a_x) + ww.//the upper right corner of the X
Var lower_left_Y2=(yy - a_y) + hh;//left corner Y
Var upper_left_X2=(xx - a_x);//the top left corner X
Var upper_left_Y2=(yy - a_y);//the top left corner Y

//application scaling on the elements
Var newScale=store. OriginScale * zoom;

If (upper_right_X & lt; Upper_right_X2 & amp; & Lower_left_Y & lt; Lower_left_Y2 & amp; & (parseInt (upper_left_X)) & gt; Upper_left_X2 & amp; & (parseInt (upper_left_Y)) & gt; Upper_left_Y2) {//determine whether beyond the boundary (the judge move offset stepped out of bounds is follow zoom ratio, inside is useless)
//the biggest scaling limit
If (newScale & gt; 2) {
NewScale=2;
}
//remember use zoom value
Store. Scale=newScale;
//image application scale effects
EleImg. Style. Transform='scale (' + newScale +') '.
}
}
});

Document. The addEventListener (' touchend ', function () {//fingers after leaving the screen to trigger
Store. The moveable=false;

The delete store. PageX2;
The delete store. PageY2;
});
Document. The addEventListener (' touchcancel ', function () {//perform all don't come in
Store. The moveable=false;
The delete store. PageX2;
The delete store. PageY2;
});
  • Related