javascript實現行拖動的方法

來源:互聯網
上載者:User

javascript實現行拖動的方法

   本文執行個體講述了javascript實現行拖動的方法。分享給大家供大家參考。具體如下:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

<!doctype html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>行拖動</title>

<script>

window.onload = function(){

//綁定事件

var addEvent = document.addEventListener ? function(el,type,callback){

el.addEventListener( type, callback, !1 );

} : function(el,type,callback){

el.attachEvent( "on" + type, callback );

}

//判定對樣式的支援

var getStyleName= (function(){

var prefixes = ['', '-ms-','-moz-', '-webkit-', '-khtml-', '-o-'];

var reg_cap = /-([a-z])/g;

function getStyleName(css, el) {

el = el || document.documentElement;

var style = el.style,test;

for (var i=0, l=prefixes.length; i < l; i++) {

test = (prefixes[i] + css).replace(reg_cap,function($0,$1){

return $1.toUpperCase();

});

if(test in style){

return test;

}

}

return null;

}

return getStyleName;

})();

var userSelect = getStyleName("user-select");

//精確擷取樣式

var getStyle = document.defaultView ? function(el,style){

return document.defaultView.getComputedStyle(el, null).getPropertyValue(style)

} : function(el,style){

style = style.replace(/\-(\w)/g, function($, $1){

return $1.toUpperCase();

});

return el.currentStyle[style];

}

var dragManager = {

y:0,

dragStart:function(e){

e = e || event;

var handler = e.target || e.srcElement;

if(handler.nodeName === "TD"){

handler = handler.parentNode;

dragManager.handler = handler;

if(!handler.getAttribute("data-background")){

handler.setAttribute("data-background",getStyle(handler,"background-color"))

}

//顯示為可移動的狀態

handler.style.backgroundColor = "#ccc";

handler.style.cursor = "move";

dragManager.y = e.clientY;

if(typeof userSelect === "string"){

return document.documentElement.style[userSelect] = "none";

}

document.unselectable = "on";

document.onselectstart = function(){

return false;

}

}

},

draging:function(e){//mousemove時拖動行

var handler = dragManager.handler;

if(handler){

e = e || event;

var y = e.clientY;

var down = y > dragManager.y;//是否向下移動

var tr = document.elementFromPoint(e.clientX,e.clientY);

if(tr && tr.nodeName == "TD"){

tr = tr.parentNode

dragManager.y = y;

if( handler !== tr){

tr.parentNode.insertBefore(handler, (down ? tr.nextSibling : tr));

}

};

}

},

dragEnd:function(){

var handler = dragManager.handler

if (handler) {

handler.style.backgroundColor = handler.getAttribute("data-background");

handler.style.cursor = "default";

dragManager.handler = null;

}

if(typeof userSelect === "string"){

return document.documentElement.style[userSelect] = "text";

}

document.unselectable = "off";

document.onselectstart = null;

},

main:function(el){

addEvent(el,"mousedown",dragManager.dragStart);

addEvent(document,"mousemove",dragManager.draging);

addEvent(document,"mouseup",dragManager.dragEnd);

}

}

var el = document.getElementById("table");

dragManager.main(el);

}

</script>

<style>

.table{width:60%;border: 1px solid red;border-collapse: collapse;}

.table td{border: 1px solid red;height: 20px;}

</style>

</head>

<body>

<h1>行拖動</h1>

<table id="table" class="table">

<tbody>

<tr><td>1</td><td>One</td><td>dom.require</td></tr>

<tr id="2" ><td class="2">2</td><td>Two</td><td>ControlJS </td></tr>

<tr id="3" ><td class="3">3</td><td>Three</td><td>HeadJS</td></tr>

<tr id="4" ><td class="4">4</td><td>Four</td><td>LAB.js</td></tr>

<tr id="5" ><td class="5">5</td><td>Five</td><td>$script.js</td></tr>

<tr id="6" ><td class="6">6</td><td>Six</td><td>NBL.js</td></tr>

</tbody>

</table>

</body>

</html>

  希望本文所述對大家的javascript程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.