Skip to content

Instantly share code, notes, and snippets.

@carolineschnapp
Created August 24, 2012 15:41
Show Gist options
  • Save carolineschnapp/3452112 to your computer and use it in GitHub Desktop.
Save carolineschnapp/3452112 to your computer and use it in GitHub Desktop.
swatches.liquid
<style>
/* Hiding the drop-downs. */
#product-variants, .selector-wrapper { display:none; }
/* Style the swatches */
#swatches { margin:20px 0 0; }
.swatch { margin:15px 0; }
.swatch ul { list-style-type:none; margin:0; padding:0; }
.swatch li {
/* Rounded corners */
-webkit-border-radius:2px;
-moz-border-radius:2px;
border-radius:2px;
/* Cross-browser inline-block */
display:-moz-inline-stack;
display:inline-block;
zoom:1;
*display:inline;
/* Content must stretch all the way to borders */
padding:0;
/* Background color */
background-color:#ddd;
/* Spacing between buttons */
margin:0px 5px 10px 0;
/* Fake that those are buttons, i.e. clicky */
cursor:pointer;
/* The border when the button is not selected */
border: #DDD 1px solid !important;
/* To position the sold out graphic and tooltip */
position:relative;
/* Text styling */
text-transform:uppercase;
font-size:13px;
}
/* Styles for the text or color container within the swatch button */
.swatch li span { display:block; margin:5px 10px; }
/* Special styles for color swatches */
/* They don't contain text so they need to have a width and height */
.swatch li.color { width:50px; height:35px; }
/* The container of the image/color must be as big as its container */
.swatch li.color span { width:100%; height:100%; margin:0; }
/* Styling sold out options */
.swatch ul.options li.soldout { cursor:default; }
/* That's the image with the cross in it */
.crossed-out { position:absolute; width:100%; height:100%; left:0; right:0; }
.swatch li .crossed-out { display:none; }
.swatch li.soldout .crossed-out { display:block; }
.swatch li.soldout span {
filter: alpha(opacity=60); /* internet explorer */
-khtml-opacity: 0.6; /* khtml, old safari */
-moz-opacity: 0.6; /* mozilla, netscape */
opacity: 0.6; /* fx, safari, opera */
}
/* Styling selected swatch */
/* Darker border and slightly raised */
.swatch li.selected {
border:#999 1px solid !important;
-webkit-box-shadow:0px 1px 2px rgba(0,0,0,0.25);
-moz-box-shadow:0px 1px 2px rgba(0,0,0,0.25);
box-shadow:0px 1px 2px rgba(0,0,0,0.25);
}
/* Bring on animated tooltips */
.swatch .options li {
-webkit-transform: translateZ(0); /* webkit flicker fix */
-webkit-font-smoothing: antialiased; /* webkit text rendering fix */
}
/* Tooltips */
.swatch .tooltip {
/* Color and text alignment */
text-align:center;
background:gray;
color:#fff;
bottom:100%;
padding: 10px;
/* Positioning */
display:block;
position:absolute;
width:100px;
left:-30px;
margin-bottom:15px;
/* Make it invisible by default */
filter:alpha(opacity=0);
-khtml-opacity: 0;
-moz-opacity: 0;
opacity:0;
visibility:hidden;
/* Animations */
-webkit-transform: translateY(10px);
-moz-transform: translateY(10px);
-ms-transform: translateY(10px);
-o-transform: translateY(10px);
transform: translateY(10px);
-webkit-transition: all .25s ease-out;
-moz-transition: all .25s ease-out;
-ms-transition: all .25s ease-out;
-o-transition: all .25s ease-out;
transition: all .25s ease-out;
-webkit-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
-moz-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
-ms-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
-o-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
}
/* This bridges the gap so you can mouse into the tooltip without it disappearing */
.swatch .tooltip:before {
bottom:-20px;
content:" ";
display:block;
height:20px;
left:0;
position:absolute;
width:100%;
}
/* CSS Triangle */
.swatch .options li .tooltip:after {
border-left: solid transparent 10px;
border-right: solid transparent 10px;
/* the color below must match the color of the
background of your tooltip */
border-top: solid gray 10px;
bottom: -10px;
content: " ";
height: 0;
left: 50%;
margin-left: -13px;
position: absolute;
width: 0;
}
/* Tooltip visible */
.swatch li:hover .tooltip {
filter: alpha(opacity=100);
-khtml-opacity: 1;
-moz-opacity: 1;
opacity: 1;
visibility: visible;
-webkit-transform: translateY(0px);
-moz-transform: translateY(0px);
-ms-transform: translateY(0px);
-o-transform: translateY(0px);
transform: translateY(0px);
}
</style>
{% for option in product.options %}
{% assign option_handle = option | handle %}
{% assign option_index = forloop.index0 %}
{% assign is_color = false %}
{% capture downcased_option %}{{ option | downcase }}{% endcapture %}
{% if downcased_option contains 'color' or downcased_option contains 'colour' %}
{% assign is_color = true %}
{% endif %}
<div class="swatch clearfix">
{% comment %}
<h5>{{ option }}</h5>
{% endcomment %}
<ul class="{{ option_handle }} options">
{% assign values = '' %}
{% for variant in product.variants %}
{% assign value = variant.options[option_index] %}
{% capture wrapped_value %},{{ value }},{% endcapture %}
{% unless values contains wrapped_value %}
<li data-option-title="{{ value | escape }}" class="{% if is_color %}color {% endif %}{{ value | handle }} {% if variant.available %}available{% else %}soldout{% endif %}">
<img class="crossed-out" src="{{ 'soldout.png' | asset_url }}" />
{% if is_color %}
<div class="tooltip">{{ value }}</div>
{% endif %}
{% if is_color %}
<span style="background-color: {{ value | split: ' ' | last | handle }}; background-image: url({{ value | handle | append: '.png' | asset_url }})"></span>
{% else %}
<span>{{ value }}</span>
{% endif %}
</li>
{% capture values %}{{ values }}{{ wrapped_value }}{% endcapture %}
{% endunless %}
{% if variant.available %}
<script>
jQuery('.options:eq({{ option_index }}) li.{{ value | handle }}').removeClass('soldout').addClass('available');
</script>
{% endif %}
{% endfor %}
</ul>
</div><!-- .swatch -->
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment