如何在jquery中變長線變長?我試圖連接兩個div,並且我需要這條線是動態的,作爲div的一個移動。因此,我需要線路變得更長。動畫變長線
Q
動畫變長線
0
A
回答
1
我假設你的行是一個HTML元素,即<div>
什麼的,所以你可以改變它的width
屬性。所以通過增加width
隨着時間的推移來增加動畫效果。
0
假設你使用的是<hr>
爲您的生產線,或者即使你只是用一個div,你可以簡單地使用jQuery的動畫:
0
你可以試試這個:
<!DOCTYPE html>
<html>
<head>
<title>demo</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"
type="text/javascript"></script>
<style type="text/css">
.drag{
position:absolute;
width:100px;
height:100px;
border:1px solid #96C2F1;
background-color: #EFF7FF;
cursor:move;
line-height:100px;
text-align:center;
}
.one{
left:100px;
top:100px;
}
.two{
left:500px;
top:100px;
}
.line{
position: absolute;
display: block;
float: left;
overflow:hidden;
}
</style>
<script type="text/javascript">
function drawQLine(x1, y1, x2, y2, color) {
var wh = (x2 - x1) || 1;
var hg = Math.abs(y2 - y1) || 1;
var up = ((y1 - y2) > 0 ? -1 : 1);
var rate;
var wm;
if (wh >= hg) {
wm = "x";
rate = wh/hg;
} else {
wm = "y"
rate = hg/wh;
}
var srate = rate - Math.floor(rate);
var sumSrate = 0;
var xOffer = x1;
var yoffer = y1;
var body = $("body");
$(".line").remove();
if (wm == "x") {
for (var i = 0; i < hg; i++) {
sumSrate += srate;
body.append($("<span class='line' style='margin-left:" + (xOffer - 7) + "px;margin-top:" + (yoffer) + "px;width:" + Math.floor(rate) + "px;height:1px;line-height:1px;background:" + color + ";'></span>"));
xOffer += Math.floor(rate);
yoffer += up;
if (sumSrate >= 1) {
body.append($("<span class='line' style='margin-left:" + (xOffer - 7) + "px;margin-top:" + (yoffer) + "px;width:1px;height:1px;line-height:1px;background:" + color + ";'></span>"));
sumSrate -= 1;
xOffer += 1;
yoffer += up;
}
}
}
if (wm == "y") {
for (var i = 0; i < wh; i++) {
sumSrate += srate;
body.append($("<span class='line' style='margin-left:" + (xOffer) + "px;margin-top:" + (yoffer) + "px;width:1px;height:" + Math.floor(rate) + "px;line-height:" + Math.floor(rate) + "px;background:" + color + ";'></span>"));
xOffer += 1;
yoffer += Math.floor(rate) * up;
if (sumSrate >= 1) {
body.append($("<span class='line' style='margin-left:" + (xOffer) + "px;margin-top:" + (yoffer) + "px;width:1px;height:1px;line-height:1px;background:" + color + ";'></span>"));
sumSrate -= 1;
xOffer += 1;
yoffer += up;
}
}
}
}
(function(document) {
$.fn.Drag = function() {
var M = false;
var Rx, Ry;
var t = $(this);
t.mousedown(function(event) {
Rx = event.pageX - (parseInt(t.css("left")) || 0);
Ry = event.pageY - (parseInt(t.css("top")) || 0);
t.css("position", "absolute").css('cursor', 'move').fadeTo(20, 0.5);
M = true;
})
.mouseup(function(event) {
M = false;
t.fadeTo(20, 1);
});
$(document).mousemove(function(event) {
if (M) {
t.css({
top: event.pageY - Ry,
left: event.pageX - Rx
});
drawConnectLine();
}
});
}
})(document);
function drawConnectLine() {
var one = $("#divOne");
var two = $("#divTwo");
drawQLine(parseInt(one.css("left")) + one.width(),
parseInt(one.css("top")) + one.height()/2,
parseInt(two.css("left")),
parseInt(two.css("top")) + two.height()/2,
"red");
}
$(document).ready(function() {
$("#divTwo").Drag();
drawConnectLine();
});
</script>
</head>
<body>
<div id="divOne" class="drag one">ONE</div>
<div id="divTwo" class="drag two">TWO</div>
</body>
</html>
+0
真的......你用很多'div'填充了路徑... – 2013-03-10 04:42:23
4
<div id="a"></div> <!--div A-->
<div id="b"></div> <!--div B-->
<div id="line"></div> <!--Line -->
$("button").click(function() {
var a = $("#a"),
b = $("#b"),
dW = b.offset().left - (a.offset().left), //dX
dH = b.offset().top - (a.offset().top), //dY
angle = Math.atan(dH/dW), //angle
length = Math.sqrt(dW * dW + dH * dH); //length in between
if(dW <0) angle += Math.PI; //some Math stuff
$("#line").css({
top: a.offset().top, //Where the line starts
left: a.offset().left,
width: 0,
rotate: angle + "rad", //rotation (prefixes not included)
transformOrigin: '0px 0px'
}).animate({
width: length //animation
}, 3000);
});
相關問題
- 1. WPF動畫線性漸變
- 2. 長線自動展開畫布
- 3. 如何在畫布上製作生長線動畫android
- 4. Ios畫線動畫
- 5. 畫布動畫畫線
- 6. WPF動畫時長
- 7. 動畫拉線
- 8. 線條動畫
- 9. 帆布畫線動畫
- 10. 在libGDX中畫線動畫
- 11. 具有漸變效果的動畫畫布線
- 12. Dimple.JS動畫線系列動畫
- 13. 動畫一圈成長
- 14. ListView長點擊動畫
- 15. HTML5動畫虛線
- 16. 集動畫曲線
- 17. HTML SVG線動畫
- 18. 動畫bezier直線
- 19. 動畫多段線失敗,緯度/長陣列
- 20. 動畫與變形動畫的區別
- 21. 鏈式變換動畫沒有動畫
- 22. 任何動畫改變UIStatusBarStyle的動畫?
- 23. Div動畫改變?
- 24. UINavigationController動畫變化
- 25. Android動畫變慢
- 26. 在動畫中畫一條線
- 27. 放鬆的動畫畫布弧線
- 28. VIM與長線(網屏線)hjkl移動
- 29. 塞格動畫黑屏/毛刺&延長滑動手勢長度
- 30. 動畫BEFORE活動變化
'我需要的線是動態的的DIV舉措之一around.'直線將無法正常工作,如果它也移動在y -軸。 – 2013-03-10 04:03:15