質問編集履歴
3
試したことを追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -145,4 +145,15 @@
|
|
145
145
|
};
|
146
146
|
}
|
147
147
|
});
|
148
|
+
```
|
149
|
+
###試したこと
|
150
|
+
ナビ以外のところをタッチしたら、ドロップダウンが消えればいいと考えました。
|
151
|
+
検索したコードに、ドロップダウンが消える処理を書きました。(その時どう書いたか忘れてしまいました)
|
152
|
+
しかし、消えた後ドロップダウンはメニューをタップしても出でこなくなりました。
|
153
|
+
```ここに言語を入力
|
154
|
+
$(document).on('click touchend', function(event) {
|
155
|
+
if (!$(event.target).closest('#target').length) {
|
156
|
+
// ここに処理;
|
157
|
+
}
|
158
|
+
});
|
148
159
|
```
|
2
タグ追加
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|
1
コード追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,9 +3,28 @@
|
|
3
3
|
メニューではない部分をタップしたら、プルダウンメニューを閉じるようにコードを追加してみたのですが(webデザイナーなので、jQueryは苦手です。)、メニューの動きも止まってしまいます。
|
4
4
|
|
5
5
|
|
6
|
-
|
6
|
+
```ここに言語を入力
|
7
|
+
jQuery(document).ready(function($){
|
8
|
+
function morphDropdown( element ) {
|
9
|
+
this.element = element;
|
10
|
+
this.mainNavigation = this.element.find('.main-nav');
|
11
|
+
this.mainNavigationItems = this.mainNavigation.find('.has-dropdown');
|
12
|
+
this.dropdownList = this.element.find('.dropdown-list');
|
13
|
+
this.dropdownWrappers = this.dropdownList.find('.dropdown');
|
14
|
+
this.dropdownItems = this.dropdownList.find('.content');
|
15
|
+
this.dropdownBg = this.dropdownList.find('.bg-layer');
|
16
|
+
this.mq = this.checkMq();
|
17
|
+
this.bindEvents();
|
18
|
+
}
|
19
|
+
|
7
|
-
morphDropdown.prototype.
|
20
|
+
morphDropdown.prototype.checkMq = function() {
|
21
|
+
//check screen size
|
8
22
|
var self = this;
|
23
|
+
return window.getComputedStyle(self.element.get(0), '::before').getPropertyValue('content').replace(/'/g, "").replace(/"/g, "").split(', ');
|
24
|
+
};
|
25
|
+
|
26
|
+
morphDropdown.prototype.bindEvents = function() {
|
27
|
+
var self = this;
|
9
28
|
//hover over an item in the main navigation
|
10
29
|
this.mainNavigationItems.mouseenter(function(event){
|
11
30
|
//hover over one of the nav items -> show dropdown
|
@@ -32,4 +51,98 @@
|
|
32
51
|
event.preventDefault();
|
33
52
|
self.showDropdown($(this));
|
34
53
|
}
|
35
|
-
});
|
54
|
+
});
|
55
|
+
|
56
|
+
//on small screens, open navigation clicking on the menu icon
|
57
|
+
this.element.on('click', '.nav-trigger', function(event){
|
58
|
+
event.preventDefault();
|
59
|
+
self.element.toggleClass('nav-open');
|
60
|
+
});
|
61
|
+
};
|
62
|
+
|
63
|
+
morphDropdown.prototype.showDropdown = function(item) {
|
64
|
+
this.mq = this.checkMq();
|
65
|
+
if( this.mq == 'desktop') {
|
66
|
+
var self = this;
|
67
|
+
var selectedDropdown = this.dropdownList.find('#'+item.data('content')),
|
68
|
+
selectedDropdownHeight = selectedDropdown.innerHeight(),
|
69
|
+
selectedDropdownWidth = selectedDropdown.children('.content').innerWidth(),
|
70
|
+
selectedDropdownLeft = item.offset().left + item.innerWidth()/2 - selectedDropdownWidth/2;
|
71
|
+
|
72
|
+
//update dropdown position and size
|
73
|
+
this.updateDropdown(selectedDropdown, parseInt(selectedDropdownHeight), selectedDropdownWidth, parseInt(selectedDropdownLeft));
|
74
|
+
//add active class to the proper dropdown item
|
75
|
+
this.element.find('.active').removeClass('active');
|
76
|
+
selectedDropdown.addClass('active').removeClass('move-left move-right').prevAll().addClass('move-left').end().nextAll().addClass('move-right');
|
77
|
+
item.addClass('active');
|
78
|
+
//show the dropdown wrapper if not visible yet
|
79
|
+
if( !this.element.hasClass('is-dropdown-visible') ) {
|
80
|
+
setTimeout(function(){
|
81
|
+
self.element.addClass('is-dropdown-visible');
|
82
|
+
}, 10);
|
83
|
+
}
|
84
|
+
}
|
85
|
+
};
|
86
|
+
|
87
|
+
morphDropdown.prototype.updateDropdown = function(dropdownItem, height, width, left) {
|
88
|
+
this.dropdownList.css({
|
89
|
+
'-moz-transform': 'translateX(' + left + 'px)',
|
90
|
+
'-webkit-transform': 'translateX(' + left + 'px)',
|
91
|
+
'-ms-transform': 'translateX(' + left + 'px)',
|
92
|
+
'-o-transform': 'translateX(' + left + 'px)',
|
93
|
+
'transform': 'translateX(' + left + 'px)',
|
94
|
+
'width': width+'px',
|
95
|
+
'height': height+'px'
|
96
|
+
});
|
97
|
+
|
98
|
+
this.dropdownBg.css({
|
99
|
+
'-moz-transform': 'scaleX(' + width + ') scaleY(' + height + ')',
|
100
|
+
'-webkit-transform': 'scaleX(' + width + ') scaleY(' + height + ')',
|
101
|
+
'-ms-transform': 'scaleX(' + width + ') scaleY(' + height + ')',
|
102
|
+
'-o-transform': 'scaleX(' + width + ') scaleY(' + height + ')',
|
103
|
+
'transform': 'scaleX(' + width + ') scaleY(' + height + ')'
|
104
|
+
});
|
105
|
+
};
|
106
|
+
|
107
|
+
morphDropdown.prototype.hideDropdown = function() {
|
108
|
+
this.mq = this.checkMq();
|
109
|
+
if( this.mq == 'desktop') {
|
110
|
+
this.element.removeClass('is-dropdown-visible').find('.active').removeClass('active').end().find('.move-left').removeClass('move-left').end().find('.move-right').removeClass('move-right');
|
111
|
+
}
|
112
|
+
};
|
113
|
+
|
114
|
+
morphDropdown.prototype.resetDropdown = function() {
|
115
|
+
this.mq = this.checkMq();
|
116
|
+
if( this.mq == 'mobile') {
|
117
|
+
this.dropdownList.removeAttr('style');
|
118
|
+
}
|
119
|
+
};
|
120
|
+
|
121
|
+
var morphDropdowns = [];
|
122
|
+
if( $('.cd-morph-dropdown').length > 0 ) {
|
123
|
+
$('.cd-morph-dropdown').each(function(){
|
124
|
+
//create a morphDropdown object for each .cd-morph-dropdown
|
125
|
+
morphDropdowns.push(new morphDropdown($(this)));
|
126
|
+
});
|
127
|
+
|
128
|
+
var resizing = false;
|
129
|
+
|
130
|
+
//on resize, reset dropdown style property
|
131
|
+
updateDropdownPosition();
|
132
|
+
$(window).on('resize', function(){
|
133
|
+
if( !resizing ) {
|
134
|
+
resizing = true;
|
135
|
+
(!window.requestAnimationFrame) ? setTimeout(updateDropdownPosition, 300) : window.requestAnimationFrame(updateDropdownPosition);
|
136
|
+
}
|
137
|
+
});
|
138
|
+
|
139
|
+
function updateDropdownPosition() {
|
140
|
+
morphDropdowns.forEach(function(element){
|
141
|
+
element.resetDropdown();
|
142
|
+
});
|
143
|
+
|
144
|
+
resizing = false;
|
145
|
+
};
|
146
|
+
}
|
147
|
+
});
|
148
|
+
```
|