回答編集履歴

4

contentsを複数配置してもいいようにした

2017/05/10 06:27

投稿

root_jp
root_jp

スコア4666

test CHANGED
@@ -34,9 +34,11 @@
34
34
 
35
35
  $(".contents_btn").on("click", function() {
36
36
 
37
- var contentsObj = $("div.contents");
37
+ var btnObj = $(this);
38
38
 
39
+ var contentsObj = btnObj.closest("div.btn_area").prev("div.contents");
40
+
39
- $(this).attr("src", contentsObj.is(":hidden") ? "img/pc/btn_close.png" : "img/pc/btn_open.png");
41
+ btnObj.attr("alt", contentsObj.is(":hidden") ? "img/pc/btn_close.png" : "img/pc/btn_open.png");
40
42
 
41
43
  contentsObj.slideToggle("slow");
42
44
 
@@ -46,6 +48,4 @@
46
48
 
47
49
  });
48
50
 
49
-
50
-
51
51
  ```

3

スライド速度をslowにした

2017/05/10 06:27

投稿

root_jp
root_jp

スコア4666

test CHANGED
@@ -38,7 +38,7 @@
38
38
 
39
39
  $(this).attr("src", contentsObj.is(":hidden") ? "img/pc/btn_close.png" : "img/pc/btn_open.png");
40
40
 
41
- contentsObj.slideToggle();
41
+ contentsObj.slideToggle("slow");
42
42
 
43
43
  $(window).scrollTop(contentsObj.offset().top);
44
44
 

2

組み込み方を追記

2017/05/10 06:22

投稿

root_jp
root_jp

スコア4666

test CHANGED
@@ -23,3 +23,29 @@
23
23
  $(window).scrollTop($(this).prev("div.contents").offset().top);
24
24
 
25
25
  ```
26
+
27
+
28
+
29
+ ###追記(動くもの)
30
+
31
+ ```JavaScript
32
+
33
+ $(function() {
34
+
35
+ $(".contents_btn").on("click", function() {
36
+
37
+ var contentsObj = $("div.contents");
38
+
39
+ $(this).attr("src", contentsObj.is(":hidden") ? "img/pc/btn_close.png" : "img/pc/btn_open.png");
40
+
41
+ contentsObj.slideToggle();
42
+
43
+ $(window).scrollTop(contentsObj.offset().top);
44
+
45
+ });
46
+
47
+ });
48
+
49
+
50
+
51
+ ```

1

追記

2017/05/10 06:19

投稿

root_jp
root_jp

スコア4666

test CHANGED
@@ -11,3 +11,15 @@
11
11
  $(window).scrollTop($(this).offset().top);
12
12
 
13
13
  ```
14
+
15
+
16
+
17
+ ###追記(ボタンを移動したくない場合)
18
+
19
+ これでどうでしょう?
20
+
21
+ ```JavaScript
22
+
23
+ $(window).scrollTop($(this).prev("div.contents").offset().top);
24
+
25
+ ```