Home » jQuery 應用

[jQ]用 jQuery 做廣告 – 仿 Yahoo 切換式 N 格圖片廣告

範例 1
範例 2
沒錯!只要 1,800 元就能獲得我們團隊完整的協助,讓效果能迅速的整合到您的網站,並保證瀏覽器的相容性。
立刻申請!

還記得筆者曾經寫過一篇仿 Yahoo 切換式兩格圖片廣告嗎?因為原本只是針對一次兩格來做效果,所以就有蠻多網友問說怎樣一次顯示超過兩格呢?剛好趁著 Yahoo! 奇摩新聞原本的效果也因為改版而有一點點不同

所以筆者此次就教各位如何寫個能 N 格切換的效果。

HTML 的部份簡單一點點了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<body>
	<div class="abgne-yahoo-carousel">
		<h3>jQuery 熱門外掛</h3>
		<span class="page"></span>
		<div class="btn">
			<a href="#" class="prev">Prev</a>
			<a href="#" class="next">Next</a>
		</div>
		<div class="frame">
			<ul>
				<li>
					<a class="thumb" href="#">
						<img title="Android" src="images/a.jpg"/>
					</a>
				</li>
				~以下省略~
			</ul>
		</div>
	</div>
</body>

接著一樣是利用 CSS 來裝飾:

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
.abgne-yahoo-carousel {
	width: 830px;
	height: 230px;
	padding: 10px;
	border: 1px solid #ccc;
	position: relative;
}
.abgne-yahoo-carousel * {
	margin: 0;
	padding: 0;
}
.abgne-yahoo-carousel ul, .abgne-yahoo-carousel li {
	list-style: none;
}
.abgne-yahoo-carousel a img {
	border: none;
	width: 198px;
}
.abgne-yahoo-carousel h3 {
	font-size: 18px;
	height: 30px;
}
.abgne-yahoo-carousel .page {
	position: absolute;
	top: 12px;
	right: 80px;
}
.abgne-yahoo-carousel .btn {
	position: absolute;
	top: 10px;
	right: 5px;
	height: 20px;
}
.abgne-yahoo-carousel .btn a {
	width: 31px;
	height: 24px;
	float: left;
	text-indent: -9999px;
}
.abgne-yahoo-carousel .btn a.prev {
	background: url(images/btn.gif) no-repeat 0 -42px;
}
.abgne-yahoo-carousel .btn a.next {
	background: url(images/btn.gif) no-repeat 0 0;
}
.abgne-yahoo-carousel .frame {
	position: relative;
	overflow: hidden;
	width: 830px;	/* (li 的寬度 + li 的邊框寬度 * 2 ) * 一次要顯示的數量 + li 的右邊界 * (一次要顯示的數量 - 1) */
	height: 200px;
}
.abgne-yahoo-carousel ul {
	width: 99999px;
	position: absolute;
}
.abgne-yahoo-carousel li {
	float: left;
	width: 198px;
	height: 200px;
	position: relative;
	margin-right: 10px;
	border: 1px solid #333;
}
.abgne-yahoo-carousel li .thumb, .abgne-yahoo-carousel li .ovrly, .abgne-yahoo-carousel li h3 {
	position: absolute;
}
.abgne-yahoo-carousel li .ovrly, .abgne-yahoo-carousel li h3 {
	width: 100%;
	height: 32px;
	line-height: 32px;
	text-align: center;
	bottom: 0;
}
.abgne-yahoo-carousel li .ovrly {
	background: #000;
}
.abgne-yahoo-carousel li h3 a {
	color: #fff;
}
.abgne-yahoo-carousel li h3 a:hover {
	color: #f90;
}

基本上當套用上 CSS 後就能看到 9 成的畫面了:

那...jQuery 只要把控制切換及移動的功能補上就可以囉:

檢視原始碼 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
$(function(){
	$('.abgne-yahoo-carousel').each(function(){
		// 先取得相關的元素及寬度等資料
		var $this = $(this), 
			$page = $this.find('.page'), 
			$btn = $this.find('.btn'), 
			_frameWidth = $this.find('.frame').width(), 
			$carousel = $this.find('ul'), 
			$items = $carousel.find('li'), 
			_itemLength = $items.length, 
			_set = Math.ceil(_frameWidth / $items.outerWidth(true)), 
			_count = Math.ceil(_itemLength / _set), 
			_width = _set * $items.outerWidth(true) * -1, 
			_speed = 400, _opacity = 0.75, _index = 0;
 
		// 用來顯示目前已顯示及總資料筆數資訊
		$page.html('1 - ' + (_set < _itemLength ? _set : _itemLength) + ' / ' + _itemLength);
 
		// 幫每一個 li 加上標題及遮罩
		$items.each(function(){
			var $this = $(this), 
				_href = $this.find('a').attr('href'), 
				_title = $this.find('img').attr('title');
 
			$this.append('<div class="ovrly"></div>' +
				'<h3>' + 
					'<a href="' + _href + '" alt="' + _title + '" title="' + _title + '">' + _title + '</a>' + 
				'</h3>').find('.ovrly').css('opacity', _opacity);
		});
 
		// 當按了上下頁的按鈕時
		$btn.find('.prev, .next').click(function(e){
			// 計算要顯示第幾組
			_index = Math.floor((e.target.className == 'prev' ? _index - 1 + _count : _index + 1) % _count);
			var _lastNum = _set * (_index + 1);
			$page.html((_set * _index + 1) + ' - ' + (_lastNum < _itemLength ? _lastNum : _itemLength) + ' / ' + _itemLength);
 
			// 進行動畫
			$carousel.stop().animate({
				left: _index * _width
			}, _speed);
 
			e.preventDefault();
		}).focus(function(){
			this.blur();
		});
	})
});

程式會透過 li 及 .frame 的寬度來計算判斷一次要顯示幾個

1
(li 的寬度 + li 的邊框寬度 * 2 ) * 一次要顯示的數量 + li 的右邊界 * (一次要顯示的數量 - 1)

所以在範例 2 中只要在 CSS 的部份額外多了一次顯示 3 個的設定:

1
2
3
.demo2, .demo2 .frame {
	width: 620px;	/* (li 的寬度 + li 的邊框寬度 * 2 ) * 一次要顯示的數量 + li 的右邊界 * (一次要顯示的數量 - 1) */
}

接著就能在不改程式的情況下一次產生多組不同顯示個數的效果囉。

範例 1 範例 2

檔案描述
基本的範例檔案(免空) 開始下載
基本的範例檔案 會員限定
加上自動輪播功能 會員限定

80 筆針對 [jQ]用 jQuery 做廣告 – 仿 Yahoo 切換式 N 格圖片廣告 的迴響

  1. 借這一個標題問一下
    想請問版大一下
    您首頁那三張很漂亮的照片的效果
    是如何做出來的?

  2. 真的嗎?
    真是太感謝你了!

    因為我剛好要用QAQ
    想好久不知道怎樣做
    (我現在是jQuery的新新手 PHP的新手......)

  3. 男丁請問一下,
    這個JQ要怎麼搭配WordPress的新文章?
    舊式圖片抓內文的,然後可以向您首頁一樣的那種效果,
    因為我不怎麼瞭繳PHP..
    所以不會做應用:(

    麻煩一下摟:)))

  4. 回上篇,男丁大,我改了一個地方,
    你看一下是不是正確的改法…如下:

    $img = $this.find('img'),
    _href = $this.attr('href'),
    _title = $img.attr('title');
    改成
    $img = $this.find('img'),
    $a = $this.find('a'),
    _href = $a.attr('href'),
    _title = $img.attr('title');

    • 抱歉~因為範例中都沒有真正的超連結才沒注意到該部份有錯。
      你這樣寫是OK的~我範例也更正了

  5. hi男丁大,
    我終於弄出來垂直式的方式滑動了,
    現在卡在一個很詭異的地方,
    可否能請教一下,就是在圖片右方的文字部份,
    始終無法「垂直置中」,單行的話用line-height方式還可以,
    但有多行的時候,就不能用這方式了…
    之後搞了很久很久…還是想不出來哪個地方影響到…
    請看這連結 http://ehome.uhome.tw
    原始檔的話就右鍵→檢視原始檔就可以看到了…
    如果還是需要整個目錄打包的話,請見以下連結
    http://ehome.uhome.tw/YahooPicAd.rar
    希望男丁大能出手點化一下小的,謝謝!!

  6. Hi.男丁大,
    可否將我上篇# 2011-08-18 15:09:50的文章移除,
    我另外改一個空間放置「垂直版」Demo頁面與程式下載點!
    下載點:http://wtkang.no-ip.info/YahooPicAd/YahooPicAd.rar
    Demo:http://wtkang.no-ip.info/YahooPicAd/
    謝謝。

發表迴響