mirror of
https://github.com/docker/docs.git
synced 2026-04-04 10:18:57 +07:00
852 lines
31 KiB
HTML
852 lines
31 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<meta name="description" content="Getting started with Docker">
|
|
<meta name="keywords" content="[beginner, tutorial, Docker]">
|
|
<title>Tag & push your image </title>
|
|
<link rel="shortcut icon" href="https://docs.docker.com/images/favicon.png" type="image/x-icon">
|
|
<link rel="stylesheet" href="../../dist/assets/css/app.css" />
|
|
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.6/animate.min.css">
|
|
<link rel="stylesheet" href="../../css/custom.css">
|
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
|
|
<script src="../../dist/assets/js/modernizr.js"></script>
|
|
</head>
|
|
<body>
|
|
<div class="off-canvas-wrap" data-offcanvas>
|
|
<div class="inner-wrap">
|
|
|
|
<a class="left-off-canvas-toggle" href="#" >
|
|
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="35px" height="35px" viewBox="0 0 35 35" enable-background="new 0 0 35 35" xml:space="preserve">
|
|
<path fill="#3597D4" d="M30.583,9.328c0,0.752-0.539,1.362-1.203,1.362H5.113c-0.664,0-1.203-0.61-1.203-1.362l0,0
|
|
c0-0.752,0.539-1.362,1.203-1.362H29.38C30.045,7.966,30.583,8.576,30.583,9.328L30.583,9.328z"/>
|
|
<path fill="#3597D4" d="M30.583,17.09c0,0.752-0.539,1.362-1.203,1.362H5.113c-0.664,0-1.203-0.61-1.203-1.362l0,0
|
|
c0-0.752,0.539-1.362,1.203-1.362H29.38C30.045,15.728,30.583,16.338,30.583,17.09L30.583,17.09z"/>
|
|
<path fill="#3597D4" d="M30.583,24.387c0,0.752-0.539,1.362-1.203,1.362H5.113c-0.664,0-1.203-0.61-1.203-1.362l0,0
|
|
c0-0.752,0.539-1.362,1.203-1.362H29.38C30.045,23.025,30.583,23.635,30.583,24.387L30.583,24.387z"/>
|
|
</svg>
|
|
</a>
|
|
<a class="button secondary small get-started-cta">Get Started</a>
|
|
<header class="main-header">
|
|
<div class="row">
|
|
<div class="large-3 columns">
|
|
<a href="../../"><img class="logo" src="../../dist/assets/images/logo.png"></a>
|
|
</div>
|
|
<div class="large-9 columns">
|
|
<ul class="nav-global">
|
|
<li><a href="https://www.docker.com/support">Support</a></li>
|
|
<li><a href="https://training.docker.com/">Training</a></li>
|
|
<li><a href="https://docs.docker.com/">Docs</a></li>
|
|
<li><a href="http://blog.docker.com/">Blog</a></li>
|
|
<li><a href="https://hub.docker.com/">Docker Hub</a></li>
|
|
<li><a class="button" href="../../mac/started/">Get Started</a></li>
|
|
</ul>
|
|
<ul class="nav-main">
|
|
<li><a href="https://www.docker.com/products">Products</a>
|
|
<ul>
|
|
<li><a href="https://www.docker.com/pricing">Pricing</a></li>
|
|
<li><a href="https://www.docker.com/whatisdocker">What is Docker?</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="https://www.docker.com/customers">Customers</a></li>
|
|
<li><a href="https://www.docker.com/community">Community</a>
|
|
<ul>
|
|
<li><a href="https://www.docker.com/community/meetups">Meetups</a></li>
|
|
<li><a href="https://www.docker.com/community/events">Events</a></li>
|
|
<li><a href="https://forums.docker.com">Forums</a></li>
|
|
<li><a href="http://www.scoop.it/t/docker-by-docker">Community News</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="https://www.docker.com/partners">Partners</a>
|
|
<ul>
|
|
<li><a href="https://www.docker.com/partners/partner-programs">Partner Programs</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="https://www.docker.com/company">Company</a>
|
|
<ul>
|
|
<li><a href="https://www.docker.com/news-and-press">News & Press</a></li>
|
|
<li><a href="https://www.docker.com/work-docker">Work at Docker</a></li>
|
|
<li><a href="https://www.docker.com/company/management">Management</a></li>
|
|
<li><a href="https://www.docker.com/company/contact">Contact</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="https://www.docker.com/open-source">Open Source</a>
|
|
<ul>
|
|
<li><a href="https://www.docker.com/contribute">Contribute</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</header>
|
|
|
|
|
|
<aside class="left-off-canvas-menu">
|
|
<ul class="off-canvas-list">
|
|
<li class="has-submenu"><a href="#">Products</a>
|
|
<ul class="left-submenu">
|
|
<li class="back"><a href="#">Back</a></li>
|
|
<li><a href="#">Pricing</a></li>
|
|
<li><a href="#">What Is Docker</a></li>
|
|
<li><a href="#">Products</a></li>
|
|
<li><a href="#">Docker Engine</a></li>
|
|
<li><a href="#">Docker Hub</a></li>
|
|
<li><a href="#">Docker Registry</a></li>
|
|
<li><a href="#">Docker Machine</a></li>
|
|
<li><a href="#">Docker Swarm</a></li>
|
|
<li><a href="#">Docker Compose</a></li>
|
|
<li><a href="#">Kitematic</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="#">Customers</a></li>
|
|
<li class="has-submenu"><a href="#">Community</a>
|
|
<ul class="left-submenu">
|
|
<li class="back"><a href="#">Back</a></li>
|
|
<li><a href="#">Community</a></li>
|
|
<li><a href="#">Meetups</a></li>
|
|
<li><a href="https://www.docker.com/community/events">Events</a></li>
|
|
<li><a href="#">Forum</a></li>
|
|
<li><a href="#">Scoop.it</a></li>
|
|
</ul>
|
|
</li>
|
|
<li class="has-submenu"><a href="#">Partners</a>
|
|
<ul class="left-submenu">
|
|
<li class="back"><a href="#">Back</a></li>
|
|
<li><a href="#">Partners</a></li>
|
|
<li><a href="https://www.docker.com/partners/partner-programs">Partners Programs</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a href="#">Company</a></li>
|
|
<li class="has-submenu"><a href="#">Open Source</a>
|
|
<ul class="left-submenu">
|
|
<li class="back"><a href="#">Back</a></li>
|
|
<li><a href="#">Open Source</a></li>
|
|
<li><a href="#">Contribute</a></li>
|
|
<li><a href="#">Governance</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
<ul class="nav-global-off-canvas">
|
|
<li><a href="#">Support</a></li>
|
|
<li><a href="#">Training</a></li>
|
|
<li><a href="#">Docs</a></li>
|
|
<li><a href="#">Blog</a></li>
|
|
<li><a href="#">Sign in</a></li>
|
|
<li><a href="#">Sign up</a></li>
|
|
</ul>
|
|
</aside>
|
|
|
|
<a class="exit-off-canvas"></a>
|
|
<div class="row">
|
|
<div class="large-3 columns" class="tutmenu">
|
|
<section id="multiple" class="tutmenu" data-accordion-group>
|
|
|
|
|
|
<section data-accordion>
|
|
|
|
<a data-link href="../../linux/started/" class=""> Get Started with Docker</a>
|
|
|
|
</section>
|
|
|
|
<section data-accordion>
|
|
|
|
<a data-link href="../../linux/step_one/" class=""> Install Docker</a>
|
|
|
|
</section>
|
|
|
|
<section data-accordion>
|
|
|
|
<a data-link href="../../linux/step_two/" class=""> Understand images & containers</a>
|
|
|
|
</section>
|
|
|
|
<section data-accordion>
|
|
|
|
<a data-link href="../../linux/step_three/" class=""> Find & run the whalesay image </a>
|
|
|
|
</section>
|
|
|
|
<section data-accordion>
|
|
|
|
<a data-link href="../../linux/step_four/" class=""> Build your own image</a>
|
|
|
|
</section>
|
|
|
|
<section data-accordion>
|
|
|
|
<a data-link href="../../linux/step_five/" class=""> Create a Docker Hub account and repository </a>
|
|
|
|
</section>
|
|
|
|
<section data-accordion>
|
|
|
|
<a data-link href="../../linux/step_six/" class=" active"> Tag & push your image</a>
|
|
|
|
</section>
|
|
|
|
<section data-accordion>
|
|
|
|
<a data-link href="../../linux/last_page/" class=""> Learning More</a>
|
|
|
|
</section>
|
|
|
|
</section>
|
|
|
|
<script>
|
|
$(document).ready(function () {
|
|
var $activeLink = $('#multiple [data-link].active');
|
|
var $accordions = $activeLink.parents('article[data-accordion]');
|
|
$($accordions.get().reverse()).each(function (index, accordion) {
|
|
var $accordion = $(accordion);
|
|
var $content = $accordion.find('[data-content]');
|
|
$accordion.addClass('open');
|
|
$content.css({'max-height': '100%'});
|
|
});
|
|
});
|
|
</script>
|
|
|
|
</div>
|
|
<div class="large-9 columns">
|
|
<section id="main">
|
|
<article id="content" class="tutorial">
|
|
<div class="linux" >
|
|
|
|
|
|
<h1 id="tag-push-and-pull-your-image">Tag, push, and pull your image</h1>
|
|
|
|
<p>In this section, you tag and push your <code>docker-whale</code> image to your newly
|
|
created repository. When you are done, you test the repository by pulling your
|
|
new image.</p>
|
|
|
|
<h2 id="step-1-tag-and-push-the-image">Step 1: Tag and push the image</h2>
|
|
|
|
<ol>
|
|
<li><p>Go back to your terminal.</p></li>
|
|
|
|
<li><p>List the images you currently have:</p>
|
|
|
|
<pre><code>$ docker images
|
|
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
|
|
docker-whale latest 7d9495d03763 38 minutes ago 273.7 MB
|
|
<none> <none> 5dac217f722c 45 minutes ago 273.7 MB
|
|
docker/whalesay latest fb434121fc77 4 hours ago 247 MB
|
|
hello-world latest 91c95931e552 5 weeks ago 910 B
|
|
</code></pre></li>
|
|
|
|
<li><p>Find the <code>IMAGE ID</code> for your <code>docker-whale</code> image.</p>
|
|
|
|
<pre><code>In this example, the id is `7d9495d03763`.
|
|
|
|
|
|
You'll notice that currently, the `REPOSITORY` shows the repository but not
|
|
the namespace for `docker-whale`. You need to include the `namespace` for
|
|
Docker Hub to associate it with your account. The `namespace` is the same as
|
|
your account name.
|
|
</code></pre></li>
|
|
|
|
<li><p>Use <code>IMAGE ID</code> and the <code>docker tag</code> command to tag your <code>docker-whale</code> image.</p>
|
|
|
|
<p>The command you type looks like this:</p>
|
|
|
|
|
|
<figure >
|
|
|
|
<img src="../../tutimg/tagger.png" />
|
|
|
|
|
|
</figure>
|
|
|
|
|
|
<p>Of course, your account name will be your own. So, you type the command with
|
|
your image’s ID and your account name and press RETURN.</p>
|
|
|
|
<pre><code>$ docker tag 7d9495d03763 maryatdocker/docker-whale:latest
|
|
</code></pre></li>
|
|
|
|
<li><p>Type the <code>docker images</code> command again to see your newly tagged image.</p>
|
|
|
|
<pre><code>$ docker images
|
|
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
|
|
maryatdocker/docker-whale latest 7d9495d03763 5 minutes ago 273.7 MB
|
|
docker-whale latest 7d9495d03763 2 hours ago 273.7 MB
|
|
<none> <none> 5dac217f722c 5 hours ago 273.7 MB
|
|
docker/whalesay latest fb434121fc77 5 hours ago 247 MB
|
|
hello-world latest 91c95931e552 5 weeks ago 910 B
|
|
</code></pre></li>
|
|
|
|
<li><p>Use the <code>docker login</code> command to log into the Docker Hub from the command line.</p>
|
|
|
|
<p>The format for the login command is:</p>
|
|
|
|
<pre><code>docker login --username=yourhubusername --password=yourpassword --email=youremail@company.com
|
|
</code></pre>
|
|
|
|
<p>So, for example:</p>
|
|
|
|
<pre><code>$ docker login --username=maryatdocker --password=uydfiad77fad --email=mary@docker.com
|
|
WARNING: login credentials saved in C:\Users\sven\.docker\config.json
|
|
Login Succeeded
|
|
</code></pre></li>
|
|
|
|
<li><p>Type the <code>docker push</code> command to push your image to your new repository.</p>
|
|
|
|
<pre><code>$ docker push maryatdocker/docker-whale
|
|
The push refers to a repository [maryatdocker/docker-whale] (len: 1)
|
|
7d9495d03763: Image already exists
|
|
c81071adeeb5: Image successfully pushed
|
|
eb06e47a01d2: Image successfully pushed
|
|
fb434121fc77: Image successfully pushed
|
|
5d5bd9951e26: Image successfully pushed
|
|
99da72cfe067: Image successfully pushed
|
|
1722f41ddcb5: Image successfully pushed
|
|
5b74edbcaa5b: Image successfully pushed
|
|
676c4a1897e6: Image successfully pushed
|
|
07f8e8c5e660: Image successfully pushed
|
|
37bea4ee0c81: Image successfully pushed
|
|
a82efea989f9: Image successfully pushed
|
|
e9e06b06e14c: Image successfully pushed
|
|
Digest: sha256:ad89e88beb7dc73bf55d456e2c600e0a39dd6c9500d7cd8d1025626c4b985011
|
|
</code></pre></li>
|
|
|
|
<li><p>Return to your profile on Docker Hub to see your new image.</p>
|
|
|
|
|
|
<figure >
|
|
|
|
<img src="../../tutimg/new_image.png" />
|
|
|
|
|
|
</figure>
|
|
</li>
|
|
</ol>
|
|
|
|
<h1 id="step-2-pull-your-new-image">Step 2: Pull your new image</h1>
|
|
|
|
<p>In this last section, you’ll pull the image you just pushed to hub. Before you
|
|
do that though, you’ll need to remove the original image from your local
|
|
machine. If you left the original image on your machine. Docker would not pull
|
|
from the hub — why would it? The two images are identical.</p>
|
|
|
|
<ol>
|
|
<li><p>Place your cursor at the prompt in your terminal window.</p></li>
|
|
|
|
<li><p>Type <code>docker images</code> to list the images you currently have on your local machine.</p>
|
|
|
|
<pre><code>$ docker images
|
|
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
|
|
maryatdocker/docker-whale latest 7d9495d03763 5 minutes ago 273.7 MB
|
|
docker-whale latest 7d9495d03763 2 hours ago 273.7 MB
|
|
<none> <none> 5dac217f722c 5 hours ago 273.7 MB
|
|
docker/whalesay latest fb434121fc77 5 hours ago 247 MB
|
|
hello-world latest 91c95931e552 5 weeks ago 910 B
|
|
</code></pre>
|
|
|
|
<p>To make a good test, you need to remove the <code>maryatdocker/docker-whale</code> and
|
|
<code>docker-whale</code> images from your local system. Removing them forces the next
|
|
<code>docker pull</code> to get the image from your repository.</p></li>
|
|
|
|
<li><p>Use the <code>docker rmi</code> to remove the <code>maryatdocker/docker-whale</code> and <code>docker-whale</code>
|
|
images.</p>
|
|
|
|
<p>You can use an ID or the name to remove an image.</p>
|
|
|
|
<pre><code>$ docker rmi -f 7d9495d03763
|
|
$ docker rmi -f docker-whale
|
|
</code></pre></li>
|
|
|
|
<li><p>Pull a new image from your repository using the <code>docker pull</code> command.</p>
|
|
|
|
<p>The command you type should include your username from Docker Hub.</p>
|
|
|
|
<pre><code> docker pull yourusername/docker-whale
|
|
</code></pre>
|
|
|
|
<p>Since the image is no longer available on your local system, Docker downloads it.</p>
|
|
|
|
<pre><code> $ docker run maryatdocker/docker-whale
|
|
Unable to find image 'maryatdocker/docker-whale:latest' locally
|
|
latest: Pulling from maryatdocker/docker-whale
|
|
eb06e47a01d2: Pull complete
|
|
c81071adeeb5: Pull complete
|
|
7d9495d03763: Already exists
|
|
e9e06b06e14c: Already exists
|
|
a82efea989f9: Already exists
|
|
37bea4ee0c81: Already exists
|
|
07f8e8c5e660: Already exists
|
|
676c4a1897e6: Already exists
|
|
5b74edbcaa5b: Already exists
|
|
1722f41ddcb5: Already exists
|
|
99da72cfe067: Already exists
|
|
5d5bd9951e26: Already exists
|
|
fb434121fc77: Already exists
|
|
Digest: sha256:ad89e88beb7dc73bf55d456e2c600e0a39dd6c9500d7cd8d1025626c4b985011
|
|
Status: Downloaded newer image for maryatdocker/docker-whale:latest
|
|
________________________________________
|
|
/ Having wandered helplessly into a \
|
|
| blinding snowstorm Sam was greatly |
|
|
| relieved to see a sturdy Saint Bernard |
|
|
| dog bounding toward him with the |
|
|
| traditional keg of brandy strapped to |
|
|
| his collar. |
|
|
| |
|
|
| "At last," cried Sam, "man's best |
|
|
\ friend -- and a great big dog, too!" /
|
|
----------------------------------------
|
|
\
|
|
\
|
|
\
|
|
## .
|
|
## ## ## ==
|
|
## ## ## ## ===
|
|
/""""""""""""""""___/ ===
|
|
~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~
|
|
\______ o __/
|
|
\ \ __/
|
|
\____\______/
|
|
</code></pre></li>
|
|
</ol>
|
|
|
|
<h2 id="where-to-go-next">Where to go next</h2>
|
|
|
|
<p>You’ve done a lot, you’ve done all of the following fundamental Docker tasks.</p>
|
|
|
|
<ul>
|
|
<li>installed Docker</li>
|
|
<li>run a software image in a container</li>
|
|
<li>located an interesting image on Docker Hub</li>
|
|
<li>run the image on your own machine</li>
|
|
<li>modified an image to create your own and run it</li>
|
|
<li>create a Docker Hub account and repository</li>
|
|
<li>pushed your image to Docker Hub for others to share</li>
|
|
</ul>
|
|
|
|
<p><a href="https://twitter.com/intent/tweet?button_hashtag=dockerdocs&text=Just%20ran%20a%20container%20with%20an%20image%20I%20built.%20Find%20it%20on%20%23dockerhub.%20Build%20your%20own%3A%20http%3A%2F%2Fgoo.gl%2FMUi7cA" class="twitter-hashtag-button" data-size="large" data-related="docker" target="_blank">Tweet your accomplishment!</a>
|
|
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?‘http’:‘https’;if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+’://platform.twitter.com/widgets.js’;fjs.parentNode.insertBefore(js,fjs);}}(document, ‘script’, ‘twitter-wjs’);</script></p>
|
|
|
|
<p>You’ve only scratched the surface of what Docker can do. Go to the next page to <a href="../../linux/last_page/">learn more</a>.</p>
|
|
|
|
<p> </p>
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<footer class="main-footer">
|
|
<div class="row">
|
|
</div>
|
|
<div class="row">
|
|
</div>
|
|
<div id="buildinfo">
|
|
Nov 3, 2015 at 7:59pm (PST)
|
|
{
|
|
"docker/compose": {
|
|
"ref": "docs",
|
|
"repos": [
|
|
"git@github.com:docker/compose.git"
|
|
],
|
|
"sha": "9c8173dbfda93baef214359991b6a8a54172f6ae"
|
|
},
|
|
"docker/docker-hub": {
|
|
"ref": "master",
|
|
"repos": [
|
|
"git@github.com:docker/hub2-demo.git"
|
|
],
|
|
"sha": "4b2e522c81c860d63b126342a6b981ac0ff1605c"
|
|
},
|
|
"docker/docker-trusted-registry": {
|
|
"ref": "docs",
|
|
"repos": [
|
|
"git@github.com:docker/dhe-deploy.git"
|
|
],
|
|
"sha": "b8988465878952f2e2c2472e8fc5fd35e5975fbf"
|
|
},
|
|
"docker/docs-base": {
|
|
"ref": "hugo-github-linking",
|
|
"repos": [
|
|
"git@github.com:docker/docs-base.git"
|
|
],
|
|
"sha": "dc98c0381a6cc311c9e3189dc78a3c7e62e5a205"
|
|
},
|
|
"docker/engine": {
|
|
"ref": "master",
|
|
"repos": [
|
|
"git@github.com:docker/docker.git"
|
|
],
|
|
"sha": "474b16af8ecfe94ec635dfac60025348d3186aa3"
|
|
},
|
|
"docker/machine": {
|
|
"ref": "master",
|
|
"repos": [
|
|
"git@github.com:docker/machine.git"
|
|
],
|
|
"sha": "786437901c0c883ecb59c1e0531654c1d89b326d"
|
|
},
|
|
"docker/opensource": {
|
|
"ref": "master",
|
|
"repos": [
|
|
"git@github.com:docker/opensource.git"
|
|
],
|
|
"sha": "0cd99bcdd876ca0293d8944980c79f32064b6354"
|
|
},
|
|
"docker/registry": {
|
|
"ref": "master",
|
|
"repos": [
|
|
"git@github.com:docker/distribution.git"
|
|
],
|
|
"sha": "a9da0e510032314910b5405acc50873ab2fa2e5a"
|
|
},
|
|
"docker/swarm": {
|
|
"ref": "master",
|
|
"repos": [
|
|
"git@github.com:docker/swarm.git"
|
|
],
|
|
"sha": "087e2452f3ec474f112b4e5b8c52b8dacb5751be"
|
|
},
|
|
"docker/tutorials": {
|
|
"ref": "master",
|
|
"repos": [
|
|
"git@github.com:docker/tutorials.git"
|
|
],
|
|
"sha": "cb55d4de0df55e22f443aac664d66f092f06c56b"
|
|
},
|
|
"docs.docker.com": {
|
|
"ref": "refs/heads/1-9-release",
|
|
"repos": [
|
|
"git@github.com:moxiegirl/docs.docker.com.git",
|
|
"git@github.com:docker/docs.docker.com.git"
|
|
],
|
|
"sha": "5878eae5de6f012c67a2a4772327c9948274c351"
|
|
},
|
|
"kitematic/kitematic": {
|
|
"ref": "master",
|
|
"repos": [
|
|
"git@github.com:kitematic/kitematic.git"
|
|
],
|
|
"sha": "e533ed35d2eab31ce528675b0665f97516b4147b"
|
|
}
|
|
} </div>
|
|
</footer>
|
|
<link rel="stylesheet" href="../../highlight/styles/github.css">
|
|
<script src="../../highlight/highlight.pack.js"></script>
|
|
<script>hljs.initHighlightingOnLoad();</script>
|
|
|
|
<script src="../../dist/assets/js/all.js"></script>
|
|
<script>
|
|
$( 'nav li:has(ul)' ).doubleTapToGo();
|
|
</script>
|
|
<script>
|
|
|
|
;(function ( $, window, document, undefined ) {
|
|
|
|
var pluginName = 'accordion',
|
|
defaults = {
|
|
transitionSpeed: 300,
|
|
transitionEasing: 'ease',
|
|
controlElement: '[data-control]',
|
|
contentElement: '[data-content]',
|
|
groupElement: '[data-accordion-group]',
|
|
singleOpen: true
|
|
};
|
|
|
|
function Accordion(element, options) {
|
|
this.element = element;
|
|
this.options = $.extend({}, defaults, options);
|
|
this._defaults = defaults;
|
|
this._name = pluginName;
|
|
this.init();
|
|
}
|
|
|
|
Accordion.prototype.init = function () {
|
|
var self = this,
|
|
opts = self.options;
|
|
|
|
var $accordion = $(self.element),
|
|
$controls = $accordion.find('> ' + opts.controlElement),
|
|
$content = $accordion.find('> ' + opts.contentElement);
|
|
|
|
var accordionParentsQty = $accordion.parents('[data-accordion]').length,
|
|
accordionHasParent = accordionParentsQty > 0;
|
|
|
|
var closedCSS = { 'max-height': 0, 'overflow': 'hidden' };
|
|
|
|
var CSStransitions = supportsTransitions();
|
|
|
|
function debounce(func, threshold, execAsap) {
|
|
var timeout;
|
|
|
|
return function debounced() {
|
|
var obj = this,
|
|
args = arguments;
|
|
|
|
function delayed() {
|
|
if (!execAsap) func.apply(obj, args);
|
|
timeout = null;
|
|
};
|
|
|
|
if (timeout) clearTimeout(timeout);
|
|
else if (execAsap) func.apply(obj, args);
|
|
|
|
timeout = setTimeout(delayed, threshold || 100);
|
|
};
|
|
}
|
|
|
|
function supportsTransitions() {
|
|
var b = document.body || document.documentElement,
|
|
s = b.style,
|
|
p = 'transition';
|
|
|
|
if (typeof s[p] == 'string') {
|
|
return true;
|
|
}
|
|
|
|
var v = ['Moz', 'webkit', 'Webkit', 'Khtml', 'O', 'ms'];
|
|
|
|
p = 'Transition';
|
|
|
|
for (var i=0; i<v.length; i++) {
|
|
if (typeof s[v[i] + p] == 'string') {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
function requestAnimFrame(cb) {
|
|
if(window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame) {
|
|
return requestAnimationFrame(cb) ||
|
|
webkitRequestAnimationFrame(cb) ||
|
|
mozRequestAnimationFrame(cb);
|
|
} else {
|
|
return setTimeout(cb, 1000 / 60);
|
|
}
|
|
}
|
|
|
|
function toggleTransition($el, remove) {
|
|
if(!remove) {
|
|
$content.css({
|
|
'-webkit-transition': 'max-height ' + opts.transitionSpeed + 'ms ' + opts.transitionEasing,
|
|
'transition': 'max-height ' + opts.transitionSpeed + 'ms ' + opts.transitionEasing
|
|
});
|
|
} else {
|
|
$content.css({
|
|
'-webkit-transition': '',
|
|
'transition': ''
|
|
});
|
|
}
|
|
}
|
|
|
|
function calculateHeight($el) {
|
|
var height = 0;
|
|
|
|
$el.children().each(function() {
|
|
height = height + $(this).outerHeight(true);
|
|
});
|
|
|
|
$el.data('oHeight', height);
|
|
}
|
|
|
|
function updateParentHeight($parentAccordion, $currentAccordion, qty, operation) {
|
|
var $content = $parentAccordion.filter('.open').find('> [data-content]'),
|
|
$childs = $content.find('[data-accordion].open > [data-content]'),
|
|
$matched;
|
|
|
|
if(!opts.singleOpen) {
|
|
$childs = $childs.not($currentAccordion.siblings('[data-accordion].open').find('> [data-content]'));
|
|
}
|
|
|
|
$matched = $content.add($childs);
|
|
|
|
if($parentAccordion.hasClass('open')) {
|
|
$matched.each(function() {
|
|
var currentHeight = $(this).data('oHeight');
|
|
|
|
switch (operation) {
|
|
case '+':
|
|
$(this).data('oHeight', currentHeight + qty);
|
|
break;
|
|
case '-':
|
|
$(this).data('oHeight', currentHeight - qty);
|
|
break;
|
|
default:
|
|
throw 'updateParentHeight method needs an operation';
|
|
}
|
|
|
|
$(this).css('max-height', $(this).data('oHeight'));
|
|
});
|
|
}
|
|
}
|
|
|
|
function refreshHeight($accordion) {
|
|
if($accordion.hasClass('open')) {
|
|
var $content = $accordion.find('> [data-content]'),
|
|
$childs = $content.find('[data-accordion].open > [data-content]'),
|
|
$matched = $content.add($childs);
|
|
|
|
calculateHeight($matched);
|
|
|
|
$matched.css('max-height', $matched.data('oHeight'));
|
|
}
|
|
}
|
|
|
|
function closeAccordion($accordion, $content) {
|
|
$accordion.trigger('accordion.close');
|
|
|
|
if(CSStransitions) {
|
|
if(accordionHasParent) {
|
|
var $parentAccordions = $accordion.parents('[data-accordion]');
|
|
|
|
updateParentHeight($parentAccordions, $accordion, $content.data('oHeight'), '-');
|
|
}
|
|
|
|
$content.css(closedCSS);
|
|
|
|
$accordion.removeClass('open');
|
|
} else {
|
|
$content.css('max-height', $content.data('oHeight'));
|
|
|
|
$content.animate(closedCSS, opts.transitionSpeed);
|
|
|
|
$accordion.removeClass('open');
|
|
}
|
|
}
|
|
|
|
function openAccordion($accordion, $content) {
|
|
$accordion.trigger('accordion.open');
|
|
if(CSStransitions) {
|
|
toggleTransition($content);
|
|
|
|
if(accordionHasParent) {
|
|
var $parentAccordions = $accordion.parents('[data-accordion]');
|
|
|
|
updateParentHeight($parentAccordions, $accordion, $content.data('oHeight'), '+');
|
|
}
|
|
|
|
requestAnimFrame(function() {
|
|
$content.css('max-height', $content.data('oHeight'));
|
|
});
|
|
|
|
$accordion.addClass('open');
|
|
} else {
|
|
$content.animate({
|
|
'max-height': $content.data('oHeight')
|
|
}, opts.transitionSpeed, function() {
|
|
$content.css({'max-height': 'none'});
|
|
});
|
|
|
|
$accordion.addClass('open');
|
|
}
|
|
}
|
|
|
|
function closeSiblingAccordions($accordion) {
|
|
var $accordionGroup = $accordion.closest(opts.groupElement);
|
|
|
|
var $siblings = $accordion.siblings('[data-accordion]').filter('.open'),
|
|
$siblingsChildren = $siblings.find('[data-accordion]').filter('.open');
|
|
|
|
var $otherAccordions = $siblings.add($siblingsChildren);
|
|
|
|
$otherAccordions.each(function() {
|
|
var $accordion = $(this),
|
|
$content = $accordion.find(opts.contentElement);
|
|
|
|
closeAccordion($accordion, $content);
|
|
});
|
|
|
|
$otherAccordions.removeClass('open');
|
|
}
|
|
|
|
function toggleAccordion() {
|
|
var isAccordionGroup = (opts.singleOpen) ? $accordion.parents(opts.groupElement).length > 0 : false;
|
|
|
|
calculateHeight($content);
|
|
|
|
if(isAccordionGroup) {
|
|
closeSiblingAccordions($accordion);
|
|
}
|
|
|
|
if($accordion.hasClass('open')) {
|
|
closeAccordion($accordion, $content);
|
|
} else {
|
|
openAccordion($accordion, $content);
|
|
}
|
|
}
|
|
|
|
function addEventListeners() {
|
|
$controls.on('click', toggleAccordion);
|
|
|
|
$controls.on('accordion.toggle', function() {
|
|
if(opts.singleOpen && $controls.length > 1) {
|
|
return false;
|
|
}
|
|
|
|
toggleAccordion();
|
|
});
|
|
|
|
$(window).on('resize', debounce(function() {
|
|
refreshHeight($accordion);
|
|
}));
|
|
}
|
|
|
|
function setup() {
|
|
$content.each(function() {
|
|
var $curr = $(this);
|
|
|
|
if($curr.css('max-height') != 0) {
|
|
if(!$curr.closest('[data-accordion]').hasClass('open')) {
|
|
$curr.css({ 'max-height': 0, 'overflow': 'hidden' });
|
|
} else {
|
|
toggleTransition($curr);
|
|
calculateHeight($curr);
|
|
|
|
$curr.css('max-height', $curr.data('oHeight'));
|
|
}
|
|
}
|
|
});
|
|
|
|
|
|
if(!$accordion.attr('data-accordion')) {
|
|
$accordion.attr('data-accordion', '');
|
|
$accordion.find(opts.controlElement).attr('data-control', '');
|
|
$accordion.find(opts.contentElement).attr('data-content', '');
|
|
}
|
|
}
|
|
|
|
setup();
|
|
addEventListeners();
|
|
};
|
|
|
|
$.fn[pluginName] = function ( options ) {
|
|
return this.each(function () {
|
|
if (!$.data(this, 'plugin_' + pluginName)) {
|
|
$.data(this, 'plugin_' + pluginName,
|
|
new Accordion( this, options ));
|
|
}
|
|
});
|
|
}
|
|
|
|
})( jQuery, window, document );
|
|
|
|
$(document).ready(function() {
|
|
$('#multiple [data-accordion]').accordion({
|
|
singleOpen: false
|
|
});
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
<noscript>
|
|
<iframe src="//www.googletagmanager.com/ns.html?id=GTM-PSVHG8"
|
|
height="0" width="0" style="display:none;visibility:hidden"></iframe>
|
|
</noscript>
|
|
<script>
|
|
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
|
|
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
|
|
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
|
|
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
|
|
})(window,document,'script','dataLayer','GTM-PSVHG8');
|
|
</script>
|
|
|