Тема: рейтинг
Конечно это не совершенство,но все работает,а главное на всех браузерах.
Все что надо-это нарисовать два рисунка,первый 20х60 это сами звезды
,второй 20х20.но с прозрачным фоном на 100%,он для background тега input в не активном состояние.Далее пишим css
dl.star-rating ol {
margin: 0;
padding: 0;
list-style: none;
width: 100px;
height: 20px;
position: relative;
background: url(stars.png);
}
dl.star-rating li {
float: left;
margin: 0;
padding: 0;
}
dl.star-rating li input{
display: block;
width: 20px;
height: 20px;
position: absolute;
z-index: 10;
}
dl.star-rating li input:hover{
background: url(stars.png) left center;height:20px;
left: 0;
z-index: 2;
}
dl.star-rating input:star1{
left: 0;
}
dl.star-rating input.star1:hover{
width: 20px;height:20px;
}
dl.star-rating input.star2{
left: 20px;
}
dl.star-rating input.star2:hover{
width: 40px;height:20px;
}
dl.star-rating input.star3{
left: 40px;
}
dl.star-rating input.star3:hover{
width: 60px;height:20px;
}
dl.star-rating input.star4{
left: 60px;
}
dl.star-rating input.star4:hover{
width: 80px;height:20px;
}
dl.star-rating input.star5{
left: 80px;
}
dl.star-rating input.star5:hover{
width: 100px;
height:20px;
}
dl.star-rating li.current {
background: url(stars.png) left bottom;
height: 20px;
z-index: 1;
}
dl ol il input{ background: url(4.gif);}
div input{background:url(stars2.png) left top; width:20px; height:20px; display:block;}
select
{
display : none;
}дальше открываем тот шаблон,где рейтинг выводится в новость,и меняем его вот на этот код
<? if (!tpl('rating', 'check')){ ?>
<dl class="star-rating">
<ol>
<li>
<? if (!tpl('rating', 'check')){ ?>
<form name="cnpostrating" method="post" style="margin: 0px; padding: 0px;">
<select size="1" name="rating[<?=$tpl['post']['id']; ?>] " style="width:0px; height:0px;">
<option value="1">1</option>
</select>
<input type="submit" value="" style="border:none;"class="star1">
</li>
</form>
<? } ?>
<? if (!tpl('rating', 'check')){ ?>
<form name="cnpostrating" method="post" style="margin: 0px; padding: 0px;">
<select size="1" name="rating[<?=$tpl['post']['id']; ?>] " style="width:0px; height:0px;">
<option value="2">2</option>
</select>
<li>
<input type="submit" value="" style="border:none;" class="star2">
</li>
</form>
<? } ?>
<? if (!tpl('rating', 'check')){ ?>
<form name="cnpostrating" method="post" style="margin: 0px; padding: 0px;">
<select size="1" name="rating[<?=$tpl['post']['id']; ?>] " style="width:0px; height:0px;">
<option value="3">3</option>
</select>
<li>
<input type="submit" value="" style="border:none;" class="star3">
</li>
</form>
<? } ?>
<? if (!tpl('rating', 'check')){ ?>
<form name="cnpostrating" method="post" style="margin: 0px; padding: 0px;">
<select size="1" name="rating[<?=$tpl['post']['id']; ?>] " style="width:0px; height:0px;">
<option value="4">4</option>
</select>
<li class="current" style="width:50px">
<input type="submit" value="" style="border:none;" class="star4"></li>
</form>
<? } ?>
<? if (!tpl('rating', 'check')){ ?>
<form name="cnpostrating" method="post" style="margin:0px; padding:0px;">
<select size="1" name="rating[<?=$tpl['post']['id']; ?>] " style="width:0px; height:0px;">
<option value="5">5</option>
</select>
<li>
<input type="submit" value="" style="border:none;" class="star5">
</form>
<? } ?>
</li>
</ol>
</dl>
<? } ?>Все просто до нельзя,но чтобы все браузеры воспринимали псевдо элимент "HOVER"
надо создать файл "csshover.htc" и засунуть его в директорию со стилями или куда вам будет удобнее.
Код файла "csshover.htc"
<attach event="ondocumentready" handler="parseStylesheets" />
<script>
var csshoverReg = /(^|\s)(([^a]([^ ]+)?)|(a([^#.][^ ]+)+)):(hover|active)/i,
currentSheet, doc = window.document, hoverEvents = [], activators = {
onhover:{on:'onmouseover', off:'onmouseout'},
onactive:{on:'onmousedown', off:'onmouseup'}
}
function parseStylesheets() {
if(!/MSIE (5|6)/.test(navigator.userAgent)) return;
window.attachEvent('onunload', unhookHoverEvents);
var sheets = doc.styleSheets, l = sheets.length;
for(var i=0; i<l; i++)
parseStylesheet(sheets[i]);
}
function parseStylesheet(sheet) {
if(sheet.imports) {
try {
var imports = sheet.imports, l = imports.length;
for(var i=0; i<l; i++) parseStylesheet(sheet.imports[i]);
} catch(securityException){}
}
try {
var rules = (currentSheet = sheet).rules, l = rules.length;
for(var j=0; j<l; j++) parseCSSRule(rules[j]);
} catch(securityException){}
}
function parseCSSRule(rule) {
var select = rule.selectorText, style = rule.style.cssText;
if(!csshoverReg.test(select) || !style) return;
var pseudo = select.replace(/[^:]+:([a-z-]+).*/i, 'on$1');
var newSelect = select.replace(/(\.([a-z0-9_-]+):[a-z]+)|(:[a-z]+)/gi, '.$2' + pseudo);
var className = (/\.([a-z0-9_-]*on(hover|active))/i).exec(newSelect)[1];
var affected = select.replace(/:(hover|active).*$/, '');
var elements = getElementsBySelect(affected);
if(elements.length == 0) return;
currentSheet.addRule(newSelect, style);
for(var i=0; i<elements.length; i++)
new HoverElement(elements[i], className, activators[pseudo]);
}
function HoverElement(node, className, events) {
if(!node.hovers) node.hovers = {};
if(node.hovers[className]) return;
node.hovers[className] = true;
hookHoverEvent(node, events.on, function() { node.className += ' ' + className; });
hookHoverEvent(node, events.off, function() { node.className = node.className.replace(new RegExp('\\s+'+className, 'g'),''); });
}
function hookHoverEvent(node, type, handler) {
node.attachEvent(type, handler);
hoverEvents[hoverEvents.length] = {
node:node, type:type, handler:handler
};
}
function unhookHoverEvents() {
for(var e,i=0; i<hoverEvents.length; i++) {
e = hoverEvents[i];
e.node.detachEvent(e.type, e.handler);
}
}
function getElementsBySelect(rule) {
var parts, nodes = [doc];
parts = rule.split(' ');
for(var i=0; i<parts.length; i++) {
nodes = getSelectedNodes(parts[i], nodes);
} return nodes;
}
function getSelectedNodes(select, elements) {
var result, node, nodes = [];
var identify = (/\#([a-z0-9_-]+)/i).exec(select);
if(identify) {
var element = doc.getElementById(identify[1]);
return element? [element]:nodes;
}
var classname = (/\.([a-z0-9_-]+)/i).exec(select);
var tagName = select.replace(/(\.|\#|\:)[a-z0-9_-]+/i, '');
var classReg = classname? new RegExp('\\b' + classname[1] + '\\b'):false;
for(var i=0; i<elements.length; i++) {
result = tagName? elements[i].all.tags(tagName):elements[i].all;
for(var j=0; j<result.length; j++) {
node = result[j];
if(classReg && !classReg.test(node.className)) continue;
nodes[nodes.length] = node;
}
}
return nodes;
}
</script>дальше надо в файле со стильми вписать правило
BODY
{
behavior :url("csshover.htc");
}и добавить в заголовки
<head>
............
<style type="text/css">
<!--[if IE]>
BODY { behavior:url("style/csshover.htc");}
<![endif]-->
</style>
.............
</head>вот и все.
результат можно посмотреть вот эдесь. я на нем тренеруюсь так что не примите за рекламуhttp://gelaxy.jino-net.ru/
Отредактировано blek (23 Apr 2007 18:29:55)