使用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 相关文章推荐
下载站控制介绍字数显示的脚本 显示全部 隐藏介绍等功能
Sep 19 Javascript
给artDialog 5.02 增加ajax get功能详细介绍
Nov 13 Javascript
JQuery伸缩导航练习示例
Nov 13 Javascript
Jquery ajaxStart()与ajaxStop()方法(实例讲解)
Dec 18 Javascript
改变隐藏的input中value值的方法
Mar 19 Javascript
JS实现用特殊符号替换字符串的中间部分区域的实例代码
Jul 24 Javascript
bootstrap动态调用select下拉框的实例代码
Aug 09 Javascript
微信小程序自定义底部导航带跳转功能
Nov 27 Javascript
详解keep-alive + vuex 让缓存的页面灵活起来
Apr 19 Javascript
微信小程序实现批量倒计时功能
Nov 01 Javascript
ant design vue datepicker日期选择器中文化操作
Oct 28 Javascript
javascript实现点击产生随机图形
Jan 25 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
php自动识别文字编码并转换为目标编码的方法
2015/08/08 PHP
Smarty高级应用之缓存操作技巧分析
2016/05/14 PHP
php基于websocket搭建简易聊天室实践
2016/10/24 PHP
php中get_magic_quotes_gpc()函数说明
2017/02/06 PHP
根据分辨率不同,调用不同的css文件
2006/08/25 Javascript
js获取控件位置以及不同浏览器中的差别介绍
2013/08/08 Javascript
jquery实现tr元素的上下移动示例代码
2013/12/20 Javascript
使用JS画图之点、线、面
2015/01/12 Javascript
jQuery实现精美的多级下拉菜单特效
2015/03/14 Javascript
innerHTML中标签可以换行的方法汇总
2015/08/14 Javascript
angularjs学习笔记之简单介绍
2015/09/26 Javascript
基于javascript实现全屏漂浮广告
2016/03/31 Javascript
微信小程序实现图片自适应(支持多图)
2017/01/25 Javascript
Vue 2中ref属性的使用方法及注意事项
2017/06/12 Javascript
IntersectionObserver实现图片懒加载的示例
2017/09/29 Javascript
vue中实现methods一个方法调用另外一个方法
2018/02/08 Javascript
JS点击动态添加标签、删除指定标签的代码
2018/04/18 Javascript
js中的数组对象排序分析
2018/12/11 Javascript
基于Vue插入视频的2种方法小结
2019/04/02 Javascript
各种Python库安装包下载地址与安装过程详细介绍(Windows版)
2016/11/02 Python
深入理解Python分布式爬虫原理
2017/11/23 Python
python3+PyQt5 数据库编程--增删改实例
2019/06/17 Python
基于Python中isfile函数和isdir函数使用详解
2019/11/29 Python
Python制作数据预测集成工具(值得收藏)
2020/08/21 Python
CSS3+JavaScript实现炫酷呼吸效果的示例代码
2020/06/15 HTML / CSS
HTML5 Canvas概述
2009/08/26 HTML / CSS
美国诺德斯特龙百货官网:Nordstrom
2016/08/23 全球购物
美国乡村商店:Plow & Hearth
2016/09/12 全球购物
西班牙国家航空官方网站:Iberia
2017/11/16 全球购物
马来西亚在线购物:POPLOOK.com
2019/12/09 全球购物
Happy Socks英国官网:购买五颜六色的袜子
2020/11/03 全球购物
法学专业毕业生自荐信范文
2013/12/18 职场文书
旅游安全协议书
2014/04/21 职场文书
五一促销活动总结
2014/07/01 职场文书
小学班主任工作总结2015
2015/04/07 职场文书
社区志愿服务活动感想
2015/08/07 职场文书