利用js实现自定义右键菜单插件


当前第2页 返回上一页

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

class RightMenu{

 constructor(param){

 this.targetId = param.targetId

 this.ele = document.querySelector('#' + this.targetId)

 console.assert(this.ele != null, '未找到id=' + this.targetId + '的元素')

 this.menu = null

 this.menuItems = param.menuItems

 this._menuItems = {}

 this.itemDefaultClass = 'item-default'

 this.event = {

 itemClick: null,

 createBefore: null

 }

 this.flag = true

 this.init()

 }

  

 init(){

 let that = this

 that.createMenu()

 this.ele.oncontextmenu = function(ee) {

 let e = ee || window.event;

 //鼠标点的坐标

 let oX = e.clientX;

 let oY = e.clientY;

 //菜单出现后的位置

 that.menu.style.display = "block";

 that.menu.style.left = oX + "px";

 that.menu.style.top = oY + "px";

 that.createMenu()

 //阻止浏览器默认事件

 return false;//一般点击右键会出现浏览器默认的右键菜单,写了这句代码就可以阻止该默认事件。

 }

 document.oncontextmenu = function(ee){

 let e = ee || window.event;

 if(e.target.id !== that.targetId && e.target.dataset.type != 'item'){

 that.menu.style.display = "none"

 }

 }

 document.onclick = function(ee) {

 let e = ee || window.event;

 that.menu.style.display = "none"

 }

 that.menu.onclick = function(ee) {

 let e = ee || window.event;

 if(e.target.dataset.type == 'item'){

 if(that.event.itemClick instanceof Function){

  that.event.itemClick(that._menuItems[e.target.id])

 }

 }

 e.cancelBubble = true;

 }

 this.menu.onmouseover = function(ee){

 that.flag = true

 }

 this.menu.onmouseleave = function(ee){

 that.flag = false

 }

 }

 createMenu(){

 if(this.menu == null){

 this.menu = document.createElement('div')

 this.menu.className = 'menu-default'

 document.body.appendChild(this.menu)

 }

  

 let mark = true

 if(this.event.createBefore instanceof Function){

 mark = this.event.createBefore()

 }

 if(mark){

 this.creatItem()

 }

 }

 _bindOncontextmenu(obj){

 obj.oncontextmenu = function(ee){

 ee.target.click()

 return false

 }

 }

 creatItem(){

 if(this.menuItems.length == 0){

 return

 }

 let fragement = document.createDocumentFragment()

 let temp = null

 let tempItem = null

 for (let i = 0, len = this.menuItems.length; i < len; i++){

 tempItem = this.menuItems[i]

 if(tempItem.show === false){

 continue

 }

 temp = document.createElement('div')

 temp.id = tempItem.id

 temp.innerHTML = tempItem.text

 temp.className = tempItem.style || 'item-default'

 temp.dataset.type = 'item'

  

 this._menuItems[tempItem.id] = tempItem

 fragement.appendChild(temp)

 this._bindOncontextmenu(temp)

 }

 this.menu.innerHTML = ''

 this.menu.appendChild(fragement)

 }

  

 on(type,event){

 this.event[type] = event

 }

  

 hide(){

 this.menu.style.display = 'none'

 }

  

 setItems(items){

 this.menuItems = items

 this.creatItem()

 }

}

推荐教程:js教程

以上就是利用js实现自定义右键菜单插件的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

浅谈javascript中相等操作符:==与===的区别

vue和node是什么关系

html css js 区别是什么

利用js实现自定义右键菜单插件

js怎么设置div的css

如何利用js获取form表单数据

javascript如何解除事件绑定

javascript操作dom对象之select(详细解答)

文件上传组件webuploader基本使用

教你一招实现“代码雨”

更多相关阅读请进入《js》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...