使用jQuery仿苹果官网焦点图特效


Posted in Javascript onDecember 23, 2014

这次我们要分享的这款jQuery焦点图非常特别,它的外观特别简单,但是又相当大气。焦点图的整体样式是仿苹果样式的,由于jQuery的运用,我们只要点击图片下方的缩略图即可达到图片切换的焦点图特效,这款jQuery焦点图插件非常适合在产片展示的网页上使用。

使用jQuery仿苹果官网焦点图特效

接下来我们一起分享一下实现这款苹果焦点图的过程及源码。

HTML代码:

<div id="gallery">

    <div id="slides" style="width: 3680px; margin-left: 0px;">

    <div class="slide"><img width="920" height="400" alt="side" src="img/sample_slides/macbook.jpg"></div>

    <div class="slide"><img width="920" height="400" alt="side" src="img/sample_slides/iphone.jpg"></div>

    <div class="slide"><img width="920" height="400" alt="side" src="img/sample_slides/imac.jpg"></div>

    <div class="slide"><a target="_blank" href="http://tutorialzine.com/2009/10/beautiful-apple-gallery-slideshow/"><img width="920" height="400" alt="side" src="img/sample_slides/info.jpg"></a></div>

    </div>

    <div id="menu">

    <ul>

        <li class="fbar inact"> </li><li class="menuItem inact act"><a href=""><img alt="thumbnail" src="img/sample_slides/thumb_macbook.png"></a></li><li class="menuItem inact"><a href=""><img alt="thumbnail" src="img/sample_slides/thumb_iphone.png"></a></li><li class="menuItem inact"><a href=""><img alt="thumbnail" src="img/sample_slides/thumb_imac.png"></a></li><li class="menuItem inact"><a href=""><img alt="thumbnail" src="img/sample_slides/thumb_about.png"></a></li>

    </ul>

    </div>

  </div>

从以上HTML代码可以看出,整个焦点图由一些div构成图片容器,用ul li列表构成下面的缩略图。

CSS代码:

#gallery{

    /* CSS3 Box Shadow */

    -moz-box-shadow:0 0 3px #AAAAAA;

    -webkit-box-shadow:0 0 3px #AAAAAA;

    box-shadow:0 0 3px #AAAAAA;

    /* CSS3 Rounded Corners */

    -moz-border-radius-bottomleft:4px;

    -webkit-border-bottom-left-radius:4px;

    border-bottom-left-radius:4px;

    -moz-border-radius-bottomright:4px;

    -webkit-border-bottom-right-radius:4px;

    border-bottom-right-radius:4px;

    border:1px solid white;

    background:url(img/panel.jpg) repeat-x bottom center #ffffff;

    /* The width of the gallery */

    width:920px;

    overflow:hidden;

}

#slides{

    /* This is the slide area */

    height:400px;

    /* jQuery changes the width later on to the sum of the widths of all the slides. */

    width:920px;

    overflow:hidden;

}

.slide{

    float:left;

}

#menu{

    /* This is the container for the thumbnails */

    height:45px;

}

ul{

    margin:0px;

    padding:0px;

}

li{

    /* Every thumbnail is a li element */

    width:60px;

    display:inline-block;

    list-style:none;

    height:45px;

    overflow:hidden;

}

li.inact:hover{

    /* The inactive state, highlighted on mouse over */

    background:url(img/pic_bg.png) repeat;

}

li.act,li.act:hover{

    /* The active state of the thumb */

    background:url(img/active_bg.png) no-repeat;

}

li.act a{

    cursor:default;

}

.fbar{

    /* The left-most vertical bar, next to the first thumbnail */

    width:2px;

    background:url(img/divider.png) no-repeat right;

}

li a{

    display:block;

    background:url(img/divider.png) no-repeat right;

    height:35px;

    padding-top:10px;

}

a img{

    border:none;

}

CSS代码也非常简单,都是一些简单设置而已。

jQuery代码:

$(document).ready(function(){

    /* This code is executed after the DOM has been completely loaded */

    var totWidth=0;

    var positions = new Array();

    $('#slides .slide').each(function(i){

        /* Traverse through all the slides and store their accumulative widths in totWidth */

        positions[i]= totWidth;

        totWidth += $(this).width();

        /* The positions array contains each slide's commulutative offset from the left part of the container */

        if(!$(this).width())

        {

            alert("Please, fill in width & height for all your images!");

            return false;

        }

    });

    $('#slides').width(totWidth);

    /* Change the cotnainer div's width to the exact width of all the slides combined */

    $('#menu ul li a').click(function(e,keepScroll){

            /* On a thumbnail click */

            $('li.menuItem').removeClass('act').addClass('inact');

            $(this).parent().addClass('act');

            var pos = $(this).parent().prevAll('.menuItem').length;

            $('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);

            /* Start the sliding animation */

            e.preventDefault();

            /* Prevent the default action of the link */

            // Stopping the auto-advance if an icon has been clicked:

            if(!keepScroll) clearInterval(itvl);

    });

    $('#menu ul li.menuItem:first').addClass('act').siblings().addClass('inact');

    /* On page load, mark the first thumbnail as active */

    /*****

     *

     *    Enabling auto-advance.

     *

     ****/

    var current=1;

    function autoAdvance()

    {

        if(current==-1) return false;

        $('#menu ul li a').eq(current%$('#menu ul li a').length).trigger('click',[true]);    // [true] will be passed as the keepScroll parameter of the click function on line 28

        current++;

    }

    // The number of seconds that the slider will auto-advance in:

    var changeEvery = 10;

    var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);

    /* End of customizations */

});

这是焦点图的重点,完成了图片滑块的动画逻辑,点击缩略图即可切换图片。

Javascript 相关文章推荐
javascript中直接写php代码的方法
Jul 31 Javascript
JavaScript 语言基础知识点总结(思维导图)
Nov 10 Javascript
javascript中不提供sleep功能如何实现这个功能
May 27 Javascript
jQuery插件Zclip实现完美兼容个浏览器点击复制内容到剪贴板
Apr 30 Javascript
js+css实现的圆角边框TAB选项卡滑动门代码分享(2款)
Aug 26 Javascript
jQuery使用$.each遍历json数组的简单实现方法
Apr 18 Javascript
AngularJS入门教程之AngularJS 模板
Aug 18 Javascript
js HTML5多图片上传及预览实例解析(不含前端的文件分割)
Aug 26 Javascript
利用Angular.js限制textarea输入的字数
Oct 20 Javascript
JS实现购物车特效
Feb 02 Javascript
详解小程序云开发数据库
May 20 Javascript
Element-ui DatePicker显示周数的方法示例
Jul 19 Javascript
jQuery分组选择器用法实例
Dec 23 #Javascript
常用的JS验证和函数汇总
Dec 23 #Javascript
jQuery后代选择器用法实例
Dec 23 #Javascript
浅析Javascript中“==”与“===”的区别
Dec 23 #Javascript
javascript实现微信分享
Dec 23 #Javascript
JSON取值前判断
Dec 23 #Javascript
jQuery基础语法实例入门
Dec 23 #Javascript
You might like
mysql+php分页类(已测)
2008/03/31 PHP
DedeCMS dede_channeltype表字段注释
2010/04/07 PHP
迁移PHP版本到PHP7
2015/02/06 PHP
php自动更新版权信息显示的方法
2015/06/19 PHP
PHP中include和require的区别实例分析
2017/05/07 PHP
深入理解JavaScript定时机制
2010/10/29 Javascript
Jquery截取中文字符串的实现代码
2010/12/22 Javascript
用nodejs访问ActiveX对象,以操作Access数据库为例。
2011/12/15 NodeJs
用Javascript来生成ftp脚本的小例子
2013/07/03 Javascript
javascript包装对象实例分析
2015/03/27 Javascript
AngularJS的表单使用详解
2015/06/17 Javascript
谈谈JavaScript中function多重理解
2015/08/28 Javascript
jQuery mobile转换url地址及获取url中目录部分的方法
2015/12/04 Javascript
基于JavaScript实现添加到购物车效果附源码下载
2016/08/22 Javascript
jQuery实现磁力图片跟随效果完整示例
2016/09/16 Javascript
JQueryMiniUI按照时间进行查询的实现方法
2017/06/07 jQuery
JavaScript原型对象、构造函数和实例对象功能与用法详解
2018/08/04 Javascript
Vue2.5学习笔记之如何在项目中使用和配置Vue
2018/09/26 Javascript
优雅的elementUI table单元格可编辑实现方法详解
2018/12/23 Javascript
vue柱状进度条图像的完美实现方案
2019/08/26 Javascript
[03:57]《不朽》——2015DOTA2国际邀请赛—中国军团出征主题曲MV
2015/07/15 DOTA
Python中针对函数处理的特殊方法
2014/03/06 Python
用pywin32实现windows模拟鼠标及键盘动作
2014/04/22 Python
浅谈Django的缓存机制
2018/08/23 Python
Python 移动光标位置的方法
2019/01/20 Python
opencv 形态学变换(开运算,闭运算,梯度运算)
2020/07/07 Python
Django web自定义通用权限控制实现方法
2020/11/24 Python
使用HTML5做的导航条详细步骤
2020/10/19 HTML / CSS
如何在Oracle中查看各个表、表空间占用空间的大小
2015/10/31 面试题
纪律教育学习心得体会
2014/09/02 职场文书
村党的群众路线教育实践活动总结材料
2014/10/31 职场文书
青少年法制教育心得体会
2016/01/14 职场文书
《鲸》教学反思
2016/02/23 职场文书
如何书写授权委托书?
2019/06/25 职场文书
2019年员工旷工保证书!
2019/06/28 职场文书
Python并发编程实例教程之线程的玩法
2021/06/20 Python