使用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数组去掉重复
May 12 Javascript
javascript时间差插件分享
Jul 18 Javascript
JavaScript中的遍历详解(多种遍历)
Apr 07 Javascript
ES6新特性一: let和const命令详解
Apr 20 Javascript
js实现加载页面就自动触发超链接的示例
Aug 31 Javascript
AngularJS中table表格基本操作示例
Oct 10 Javascript
浅谈VUE监听窗口变化事件的问题
Feb 24 Javascript
jQuery层叠选择器用法实例分析
Jun 28 jQuery
vue子路由跳转实现tab选项卡
Jul 24 Javascript
Vue最新防抖方案(必看篇)
Oct 30 Javascript
vue element-ui实现动态面包屑导航
Dec 23 Javascript
详解vue-cli项目在IE浏览器打开报错解决方法
Dec 10 Vue.js
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
全国FM电台频率大全 - 22 重庆市
2020/03/11 无线电
smtp邮件发送一例
2006/10/09 PHP
PHP FOR MYSQL 代码生成助手(根据Mysql里的字段自动生成类文件的)
2011/07/23 PHP
解析mysql 表中的碎片产生原因以及清理
2013/06/22 PHP
PHP将回调函数作用到给定数组单元的方法
2014/08/19 PHP
php使用CutyCapt实现网页截图保存的方法
2016/10/03 PHP
PHP解压ZIP文件到指定文件夹的方法
2016/11/17 PHP
php+js实现百度地图多点标注的方法
2016/11/30 PHP
PHP 文件锁与进程锁的使用示例
2017/08/07 PHP
新浪刚打开页面出来的全屏广告代码
2007/04/02 Javascript
json+jQuery实现的无限级树形菜单效果代码
2015/08/27 Javascript
初步使用Node连接Mysql数据库
2016/03/03 Javascript
Bootstrap 粘页脚效果
2016/03/28 Javascript
JavaScript 经典实例日常收集整理(常用经典)
2016/03/30 Javascript
详解Node.js模板引擎Jade入门
2018/01/19 Javascript
基于游标的分页接口实现代码示例
2018/11/12 Javascript
详解用Webpack与Babel配置ES6开发环境
2019/03/12 Javascript
ES6 Map结构的应用实例分析
2019/06/26 Javascript
微信小程序入口场景的问题集合与相关解决方法
2019/06/26 Javascript
JavaScript中的各种宽高属性的实现
2020/05/08 Javascript
vue组件系列之TagsInput详解
2020/05/14 Javascript
详细介绍Python的鸭子类型
2016/09/12 Python
Python中字典的浅拷贝与深拷贝用法实例分析
2018/01/02 Python
python 移动图片到另外一个文件夹的实例
2019/01/10 Python
PyTorch中topk函数的用法详解
2020/01/02 Python
Python namedtuple命名元组实现过程解析
2020/01/08 Python
利用python绘制数据曲线图的实现
2020/04/09 Python
python中threading和queue库实现多线程编程
2021/02/06 Python
html2canvas截图空白问题的解决
2020/03/24 HTML / CSS
网上常见的一份Linux面试题(多项选择部分)
2014/09/09 面试题
高中生学习的自我评价
2013/12/14 职场文书
给朋友的道歉信
2014/01/09 职场文书
个人校本研修方案
2014/05/26 职场文书
班级活动总结格式
2014/08/30 职场文书
酒店优秀员工推荐信
2015/03/24 职场文书
致创业您:正能量激励人心句子(48条)
2019/08/15 职场文书