Skip to content

Instantly share code, notes, and snippets.

@RyoSugimoto
Last active December 15, 2015 08:19
Show Gist options
  • Save RyoSugimoto/5230464 to your computer and use it in GitHub Desktop.
Save RyoSugimoto/5230464 to your computer and use it in GitHub Desktop.
CSSの「top」と「left」プロパティを自動的に取得するFireWorksスクリプト。
/*
*
*/
(function (fw) {
if (!fw.selection || fw.selection.length == 0) {
alert('Run it again after selection of targets.');
return false;
}
var object = fw.selection,
objectLength = object.length,
defaultPrefix = '.position',
prefix = prompt('Input the prefix for CSS selector.\nIf you don\'t input a character or cancel, it outputs \"#object.name\" or \"#object.baseName\" as the prefix.\n ', defaultPrefix),
indexLength = 3,
getSelector = null,
text = '';
function fixNum(num, length){
var str = String(num),
i = length - 1;
while (i > 0) {
if (str.length < length) {
str = '0' + str;
} else {
break;
}
i--;
}
return str;
};
if (!prefix) {
getSelector = function (object, i) {
var selector;
if (object.baseName) {
selector = '#' + object.baseName;
} else if (object.name) {
selector = '#' + object.name + '-' + fixNum(i, indexLength);
} else {
selector = defaultPrefix + '-' + fixNum(i, indexLength);
}
return selector;
};
} else {
getSelector = function (object, i) {
return prefix + '-' + fixNum(i, indexLength);
};
}
for (var i = 0; i < objectLength; i++) {
var selector = getSelector(object[i], i + 1);
text += selector + ' {\n top: ' + object[i].top + 'px;\n left: ' + object[i].left + 'px;\n}\n';
}
prompt('Copy this and paste on your CSS file.', text);
}(fw));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment