Skip to content

Instantly share code, notes, and snippets.

@creamidea
Last active December 28, 2015 15:49
Show Gist options
  • Save creamidea/7524636 to your computer and use it in GitHub Desktop.
Save creamidea/7524636 to your computer and use it in GitHub Desktop.
旋转木马 简单实现
"use strict"
( ($) ->
loadImage = ($image, src, callback) ->
$image.bind "load", (e) ->
$image.unbind "load"
callback $image
.each ->
if $image[0].complete
$image.trigger "load"
if $.browser.webkit
$image.attr 'src', ''
$image.attr 'src', src
createItem = ($image, angle, options) ->
loaded = false
sizeRange = (1 - options.minScale) * 0.5
orgWidth = orgHeight = 0
$image.css
opacity: 0
position: 'absolute'
$originDiv = $image.wrap('<div style="position: absolute;">').parent()
loadImage $image, $image.attr('src'), ($image) ->
loaded = true
orgWidth = $image.width()
orgHeight = $image.height()
$image.animate
opacity: 1
, 1000
that =
update: (ang) ->
ang += angle
sinVal = Math.sin(ang)
# 图形变换核心的三句代码
scale = ((sinVal + 1) * sizeRange) + options.minScale
x = ((Math.cos(ang) * options.radiusX) * scale) + options.width / 2
y = ((sinVal * options.radiusY) * scale) + options.width / 2
# 这里是控制图片的旋转,外层div
$originDiv.css
left: (x >> 0) + 'px'
top: (y >> 0) + 'px'
zIndex: (scale * 100) >> 0
# 这里只是判断图片是否被加载
# 这里只是图片在原地变大缩小,不会旋转
if loaded
$image.css
width: (orgWidth * scale) + 'px'
height: (orgHeight * scale) + 'px'
top: ((-orgHeight * scale) / 2 ) + 'px'
left: ((-orgWidth * scale) / 2 ) + 'px'
return that
createCarousel = ($wrap, options) ->
items = []
rot = 0
pause = false
unpauseTimeout = 0
rotAmount = (Math.PI * 2) * (options.frameRate/options.rotRate)
$images = $('img', $wrap)
spacing = (Math.PI / $images.length) * 2
angle = Math.PI / 2
$wrap.bind 'mouseover mouseout', (e) ->
if !$(e.target).is('img')
return
if e.type is 'mouseover'
clearTimeout unpauseTimeout
pause = true
else
unpauseTimeout = setTimeout ->
pause = false
, 200
for image in $images
console.log image
item = createItem $(image), angle, options
items.push item
angle += spacing
setInterval ->
if !pause
rot += rotAmount
for item in items
item.update rot
, options.frameRate
$.fn.Carousel = (options) ->
options = $.extend {}, $.fn.Carousel.defaults, options
# this.each ->
# options = $.extend {}, $.fn.Carousel.defaults, options
$(this).css
position: 'relative'
width: options.width + 'px'
height: options.height + 'px'
createCarousel $(this), options
$.fn.Carousel.defaults =
radiusX: 230
radiusY: 80
width: 512
height: 300
frameRate: 30
rotRate: 5000
minScale: 0.60
return
)(jQuery)
$ ->
$('.carousel').Carousel
width: 512
height: 300
radiusX: 220
radiusY: 70
minScale: 0.6
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>Carousel</title>
<meta name="description" content=""></meta>
<meta name="viewport" content="width=device-width"></meta>
<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="app.js"></script>
</head>
<body>
<h1>Carousel</h1>
<div class="carousel" ><!-- This is the wrapping element -->
<a href="http://en.wikipedia.org/wiki/Self-portrait_(Leonardo_da_Vinci)" target="_blank">
<img src="pic1.png" alt="Pic 1"/>
</a>
<img id="pic2" src="pic2.png" alt="Pic 2"/>
<img src="pic3.png" alt="Pic 3"/>
<img src="pic4.png" alt="Pic 4"/>
<img src="pic5.png" alt="Pic 5"/>
<img src="pic6.png" alt="Pic 6"/>
<img src="pic7.png" alt="Pic 7"/>
<img src="pic8.png" alt="Pic 8"/>
<img src="pic9.png" alt="Pic 9"/>
</div>
<footer>
<address>
<a href="mailto:creamidea%40gmail.com">creamidea</a>
</address>
</footer>
</body>
</html>
@creamidea
Copy link
Author

核心的三句代码:

scale = ((sinVal + 1) * sizeRange) + options.minScale
x = ((Math.cos(ang) * options.radiusX) * scale) + options.width / 2
y = ((sinVal * options.radiusY) * scale) + options.width / 2

然后图形更新使用一个定时器。
unpauseTimeout用于延时触发,避免误触发

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment