https://www.synergy-marketing.co.jp/
上記のサイトのように
メインビジュアルの背景とテキストを自動でかつ左右のボタンクリックで切り替えるようにしたいのですが、
どのようにしたらよいのでしょうか?
現在は下記のコードで自動でフェードイン・アウトで切り替えするところはできておりますが、
ここからボタンを追加して切り替える方法がわからない状態です。
html
1<div id="main_visual"> 2 <div class="inner"> 3 <div class="inner_bg_top"></div> 4 <p class="inner_bg"></p> 5 <div class="catch-left"> 6 <ul class="catch-left-title"> 7 <li>テキスト01</li> 8 <li>テキスト02</li> 9 <li>テキスト03</li> 10 </ul> 11 <ul class="catch-left-text"> 12 <li>aaaaaaaaaa</li> 13 <li>bbbbbbbbbb</li> 14 <li>cccccccccc</li> 15 </ul> 16 </div> 17 </div> 18 </div>
js
1//背景自動切り替え 2var speed = 1000; // フェードイン・フェードアウトの処理時間(1000で1秒) 3var times = 5000; // 画像切り替えの間隔(1000で1秒) 4var bgimages = [ 5'images_c/mainvisual_cafe.png', 6'images_c/mainvisual_school.png', 7'images_c/mainvisual_consulting.png' // 最後に,をつけない 8]; 9var position = ['0','0', '0']; 10$(function(){ 11// 現在の背景画像番号格納する変数 12var thisnum = 0; 13// 定期的に実行 14setInterval(function(){ 15// 背景をフェードアウト 16$("#main_visual .inner .inner_bg").fadeOut(speed,function(){ 17// 背景画像番号変更 18thisnum = thisnum === bgimages.length - 1 ? 0 : thisnum + 1; 19// 背景画像を変更しフェードイン 20$(this).css({"background-image":"url("+bgimages[thisnum]+")","background-position":position[thisnum] + " 0"}).fadeIn(speed); 21 22}); 23}, times); 24}); 25 26//テキスト自動切り替え 27$.fn.autoChange = function(config) { 28 // オプション 29 var options = $.extend({ 30 effect : 'fade', 31 type : 'repaet', 32 timeout : 3000, 33 speed : 1000 34 }, config); 35 36 return this.each(function() { 37 // カウンター初期化 38 var current = 0; 39 var next = 1; 40 41 // 指定した要素の子要素を取得 42 var element = $(this).children(); 43 44 // 全ての要素を非表示にする 45 $(element).hide(); 46 47 // 最初の要素だけ表示する 48 $(element[0]).show(); 49 50 // 要素を切り替えるスクリプト 51 var change = function(){ 52 // フェードしながら切り替える場合 53 if (options.effect == 'fade') { 54 $(element[current]).fadeOut(options.speed); 55 $(element[next]).fadeIn(options.speed); 56 57 // スライドしながら切り替える場合 58 } else if (options.effect == 'slide') { 59 $(element[current]).slideUp(options.speed); 60 $(element[next]).slideDown(options.speed); 61 } 62 63 // リピートする場合 64 if (options.type == 'repeat') { 65 if ((next + 1) < element.length) { 66 current = next; 67 next++; 68 } else { 69 current = element.length - 1; 70 next = 0; 71 } 72 } 73 74 // 最後の要素でストップする場合 75 if (options.type == 'stop') { 76 if ((next + 1) < element.length) { 77 current = next; 78 next++; 79 } else { 80 return; 81 } 82 } 83 }; 84 85 // 設定時間毎にスクリプトを実行 86 var timer = setInterval(function(){change();}, options.timeout); 87 }); 88}; 89 90 91// 自動切り替えする要素の設定 92 jQuery(function($){ 93 $('.catch-left-title').autoChange({effect : 'fade', type : 'repeat', timeout: 5000, speed : 2000}); 94 $('.catch-left-text').autoChange({effect : 'fade', type : 'repeat', timeout: 5000, speed : 2000}); 95});
ご教示宜しくお願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/10 01:43