Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" href="css/halfstyle.css"/>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/halfstyle.js" ></script>
<title>HalfStyle - Style each half of a character, independently and individually.</title>
</head>
Expand Down
25 changes: 11 additions & 14 deletions js/halfstyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@
* Copyright 2014 Arbel Hakopian
* Licensed under MIT (https://github.com/arbelh/HalfStyle/blob/master/license.md)
*/
jQuery(function($) {
window.onload = function(){ initHalfStyle(); };
function initHalfStyle() {
var halfstyle_text, halfstyle_chars, $halfstyle_el, halfstyle_i, halfstyle_output, halfstyle_style;

// Iterate over all class occurrences
$('.textToHalfStyle').each(function(idx, halfstyle_el) {
$halfstyle_el = $(halfstyle_el);
halfstyle_style = $halfstyle_el.data('halfstyle');
halfstyle_text = $halfstyle_el.text();
var domsToIterate = document.getElementsByClassName('textToHalfStyle');
for(var i = 0; i < domsToIterate.length; i = i + 1) {
halfstyle_el = domsToIterate[i];
halfstyle_style = halfstyle_el.attributes['data-halfstyle'].value;
halfstyle_text = halfstyle_el.innerText;
halfstyle_chars = halfstyle_text.split('');

// Set the screen-reader text
$halfstyle_el.html('<span style="position: absolute !important;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);">' + halfstyle_text + '</span>');

// Reset output for appending
halfstyle_output = '';
halfstyle_output = '<span style="position: absolute !important;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);">' + halfstyle_text + '</span>';

// Iterate over all chars in the text
for (halfstyle_i = 0; halfstyle_i < halfstyle_chars.length; halfstyle_i++) {
// Create a styled element for each character and append to container
halfstyle_output += '<span aria-hidden="true" class="halfStyle ' + halfstyle_style + '" data-content="' + halfstyle_chars[halfstyle_i] + '">' + halfstyle_chars[halfstyle_i] + '</span>';
}

// Write to DOM only once
$halfstyle_el.append(halfstyle_output);
});
});
halfstyle_el.innerHTML = halfstyle_output;
}
};
Loading