使用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中定义对象类别
Dec 22 Javascript
执行iframe中的javascript方法
Oct 07 Javascript
ExtJs纵坐标值重复问题的解决方法
Feb 27 Javascript
SeaJS入门教程系列之使用SeaJS(二)
Mar 03 Javascript
Javascript实现的SHA-256加密算法完整实例
Feb 02 Javascript
微信小程序 前端源码逻辑和工作流详解
Oct 08 Javascript
node.js Sequelize实现单实例字段或批量自增、自减
Dec 08 Javascript
微信小程序开发的四十个技术窍门总结(推荐)
Jan 23 Javascript
Three.JS实现三维场景
Dec 30 Javascript
深入学习TypeScript 、React、 Redux和Ant-Design的最佳实践
Jun 17 Javascript
Vue中computed及watch区别实例解析
Aug 01 Javascript
JavaScript选择器函数querySelector和querySelectorAll
Nov 27 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
关于查看MSSQL 数据库 用户每个表 占用的空间大小
2013/06/21 PHP
PHP fopen()和 file_get_contents()应用与差异介绍
2014/03/19 PHP
前端必学之PHP语法基础
2016/01/01 PHP
PHP-FPM和Nginx的通信机制详解
2019/02/01 PHP
关于javascript 回调函数中变量作用域的讨论
2009/09/11 Javascript
js下获取div中的数据的原理分析
2010/04/07 Javascript
JavaScript中:表达式和语句的区别[译]
2012/09/17 Javascript
JS调用CS里的带参方法实例
2013/08/01 Javascript
使用jquery中height()方法获取各种高度大全
2014/04/02 Javascript
学习JavaScript设计模式(单例模式)
2015/11/26 Javascript
Angular.js中$apply()和$digest()的深入理解
2016/10/13 Javascript
Angularjs 与 bower安装和使用详解
2017/05/11 Javascript
关于vue-router的那些事儿
2018/05/23 Javascript
小程序getLocation需要在app.json中声明permission字段
2019/04/04 Javascript
微信小程序自定义联系人弹窗
2020/05/26 Javascript
通过angular CDK实现页面元素拖放的步骤详解
2020/07/01 Javascript
原生js实现滑块区间组件
2021/01/20 Javascript
关于better-scroll插件的无法滑动bug(2021通过插件解决)
2021/03/01 Javascript
[00:17]游戏风云独家报道:DD赛后说出数字秘密 吓死你们啊!
2014/07/13 DOTA
python实现下载文件的三种方法
2017/02/09 Python
pycharm中连接mysql数据库的步骤详解
2017/05/02 Python
python中urlparse模块介绍与使用示例
2017/11/19 Python
TensorFlow实现卷积神经网络CNN
2018/03/09 Python
python-序列解包(对可迭代元素的快速取值方法)
2019/08/24 Python
python识别文字(基于tesseract)代码实例
2019/08/24 Python
python实现控制台输出彩色字体
2020/04/05 Python
python 代码实现k-means聚类分析的思路(不使用现成聚类库)
2020/06/01 Python
Jupyter Notebook安装及使用方法解析
2020/11/12 Python
Css3新特性应用之形状总结
2016/12/08 HTML / CSS
英国排名第一的最新设计师品牌手表独立零售商:TIC Watches
2016/09/24 全球购物
采购主管的岗位职责
2013/12/17 职场文书
上课迟到检讨书
2014/02/19 职场文书
解除劳动合同证明书
2014/09/26 职场文书
2016年社区“6.26”禁毒日宣传活动总结
2016/04/05 职场文书
2016年小学六一儿童节活动总结
2016/04/06 职场文书
mysql分组后合并显示一个字段的多条数据方式
2022/01/22 MySQL