jQuery插件ColorBox的参数设置 p

官方网站:http://colorpowered.com/colorbox/

  • 支持 照片,照片组,幻灯片,ajax,内联 和 iframe 框架。
  • 通过CSS 控制外观,使用用户可以很容易重新定制外观。
  • 不需要更改 ColorBox 的 javascript 文件就可以重新设定其行为。
  • 可以依靠 callback & event-hooks 进行拓展,不需要修改源代码。
  • 非常友好,不需要修改现有的 HTML,所有的选项都通过 JS 设置。

    介绍

    colorbox()函数使用一堆key/value对象和一个可选的callback函数

    格式:$('selector').colorbox({key:value}, callback);

    例子: $('a.gallery').colorbox({transition:'fade', speed:500});

    还是例子:$('button').colorbox({href:"thankyou.html"});

    设置的值

    默认值

    介绍

    transition

    "elastic"

    过渡效果,可以是"elastic", "fade", or "none".

    speed

    350

    设置过渡效果的持续时间,毫秒

    href false

    Example:$('h1').colorbox({href:"welcome.html"})

    这个用来设置一个锚标记的值或者一个不是锚的元素,例如图像或者表单的按钮,例如:

    title false

    这可以为Colorbox设置一个标题

    rel false

    Example:$('#example a').colorbox({rel:'group1'})

    这个可以根据元素的rel属性设置要显示的元素集合,也可以覆盖一个存在的rel属性

    width false

    Example: "100%", "500px", or 500

    设置宽度,包括边框和按钮

    height false

    Example: "100%", "500px", or 500

    设置高度,包括边框和按钮

    innerWidth false

    Example: "50%", "500px", or 500

    这个可以设定一个固定的内大小,包括边框和按钮

    innerHeight false

    Example: "50%", "500px", or 500

    这个可以设定一个固定的内高度,包括边框和按钮

    initialWidth 300

    设置初始化宽度

    initialHeight 100

    设置初始化高度

    maxWidth false

    Example: "100%", 500, "500px"

    设置内容的最大宽度

    maxHeight false

    Example: "100%", 500, "500px"

    设置内容的最大高度

    scalePhotos true

    如果是true且maxWidth, maxHeight, innerWidth, innerHeight, width, 或者 height 被设置,

    Colorbox会缩放图片以使用边框

    scrolling true

    如果是false,Colorbox不会为了溢出元素设置滚动条

    iframe false

    如果是true,元素会在Iframe中显示

    inline false

    Example: $("#inline").colorbox({inline:true, href:"#myForm"});

    如果是true,jQuery选择器可以用来选择要显示的元素。例如:

    html false

    Example:
    $.fn.colorbox({html:'

    这个是直接让你显示HTML代码,例

    Hello

    '});
    photo false

    如果为true,ColorBox只会把元素按照图片显示,防止类似photo.php?pic=1这样的连接被误认为是网页

    opacity 0.85

    遮罩层不透明度 从0-1之间取值

    open false

    如果为true,ColorBox会自动开启

    preloading true

    如果为True,ColorBox会自动预载要显示图片

    overlayClose true

    为true单击遮罩层就可以把ColorBox关闭

    slideshow false

    为True,会自动滚动图片

    slideshowSpeed 2500

    设置时间,毫秒

    slideshowAuto true

    为tuue,滑动会自动开始

    slideshowStart "start slideshow"

    开始自动滑动按钮的文本

    slideshowStop "stop slideshow"

    停止自动滑动按钮的文本

    current "{current} of {total}"

    文本内容:现在正在显示的元素序号

    previous "previous"

    “上一个”按钮的文本

    next "next"

    “下一个”按钮的文本

    close "close"

    “关闭”按钮的文本

一. Colorbox插件介绍

jQuery Colorbox是一款非常好的内容播放插件。它集弹出层、幻灯片播放功能于一身,不仅于此,它还支持其它的内容格式:例如html, flash, iframe等,这些内容的显示方式都是Ajax的。更难能可贵的是,在压缩后它只有10K的大小,使用这款插件不会给你的网页带来过多的负担,而且还能 实现很棒的视觉效果,为用户体验增色不少。

本文已经同步至我的个人博客站点:积累吧 | jQuery Colorbox插件

Colorbox项目的主页地址是:http://www.jacklmoore.com/colorbox
项目主页中提供了5种不同样式的Example:

Example 1:http://www.jacklmoore.com/colorbox/example1/
Example 2:http://www.jacklmoore.com/colorbox/example2/
Example 3:http://www.jacklmoore.com/colorbox/example3/
Example 4:http://www.jacklmoore.com/colorbox/example4/
Example 5:http://www.jacklmoore.com/colorbox/example5/

每一个Example中都提供了以下效果:

  • Elastic Transition(弹性动画)
  • Fade Transition(淡入淡出动画)
  • No transition + fixed width and height  75% of screen size (无动画,宽高以屏幕的75%自适应)
  • Slideshow(幻灯片播放)
  • Other Content Types (其它类型:外部html, flash和视频,iframe的flash和视频,iframe的外部html,内部html)

二. Colorbox插件使用

在使用colorbox之前,先看看基本的效果图吧。(本博客已经使用了colorbox插件的最新版本,您也可以直接点击图片看到效果哦。)

image

易于使用也是Colorbox的众多优点之一,它的代码调用非常之简洁。

1. 引入jquery和colorbox的js文件

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="colorbox/jquery.colorbox.js"></script>

2. 引入colorbox的css文件

<link media="screen" rel="stylesheet" href="colorbox\colorbox.css" />

3. html代码

<body>
        <h1>ColorBox演示</h1>
        
        <h2>弹性效果</h2>
        <p><a class="group1" data-rel='hi' href="../content/ohoopee1.jpg" title="Me and my grandfather on the Ohoopee.">Grouped Photo 1</a></p>
        <p><a class="group1" href="../content/ohoopee2.jpg" title="On the Ohoopee as a child">Grouped Photo 2</a></p>
        <p><a class="group1" href="../content/ohoopee3.jpg" title="On the Ohoopee as an adult">Grouped Photo 3</a></p>
    </body>

4. 调用colorbox的代码

<script>
    $(document).ready(function(){
        //元素调用Colorbox的示例
        $(".group1").colorbox({ rel: 'group1' });
    });
</script>

5. 最简单示例的完整代码

以上是如何使用的每一个步骤,这里是对上面4个步骤的整合后的完整示例。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>ColorBox 示例</title>
        <link rel="stylesheet" href="colorbox/colorbox.css" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script src="colorbox/jquery.colorbox.js"></script>
<script>
    $(document).ready(function(){
        //元素调用Colorbox的示例
        $(".group1").colorbox({ rel: 'group1' });
    });
</script>
    </head>
    <body>
        <h1>ColorBox演示</h1>
        
        <h2>弹性效果</h2>
        <p><a class="group1" data-rel='hi' href="../content/ohoopee1.jpg" title="Me and my grandfather on the Ohoopee.">Grouped Photo 1</a></p>
        <p><a class="group1" href="../content/ohoopee2.jpg" title="On the Ohoopee as a child">Grouped Photo 2</a></p>
        <p><a class="group1" href="../content/ohoopee3.jpg" title="On the Ohoopee as an adult">Grouped Photo 3</a></p>
    </body>
</html>



三、将它整合到Wordpress中

众多使用wordpress的朋友可能比较关心如何将colorbox插件整合到wordpress中去,而不需要到wordpress官方网站上搜索这类插件去安装。

1. 目录结构变更

在colorbox项目主页中下载完插件后,打开它,可以看到它的目录结构如下两幅图

image

image

这样的目录结构直接放到wordpress下明显不够合理,在当前目录结构下创建一个文件夹colorbox

image

然后将images文件夹、colorbox.css、jquery.colorbox.js、jquery.colorbox-min.js文件放到colorbox文件夹中

image

2. 将colorbox文件夹放到主题文件夹下

image

3. 修改header.php文件

在<head></head>之间添加下面的代码:

<link rel="stylesheet" href="http://www.jileiba.com/wp-content/themes/h/colorbox/colorbox.css" type="text/css" media="screen" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="<?php bloginfo('stylesheet_directory');?>/colorbox/jquery.colorbox-min.js"></script>
<script type="text/javascript">
    jQuery(function ($) {
        $("a[rel=ignition]").colorbox({ rel: 'ignition' });
    });
</script>
原文地址:https://www.cnblogs.com/anyuqi/p/3023612.html