2013-01-24 108 views
0

我有一个适用于滑块的脚本。但现在我需要像这样的HTML旋转箭头图像:切换箭头图像切换隐藏div jQuery脚本

http://jsfiddle.net/nicorellius/gsDVS/

我使用的图像是目前类,byline背景图像。

jQuery的脚本是在这里:

// expanding, collapsing div script 
$(document).ready(function() { 
    $(".toggle-content").hide(); 
    $(".byline").click(function() { 
     //$(this).attr("id", "down-arrow"); 
     var $next = $(this).next(".toggle-content"); 
     $(".toggle-content").not($next).slideUp(); 
     $next.slideToggle(500); 
     //$next.attr("id", ""); 
    }); 
}); 

这个工程来改变图像从左下来,例如,从iddown-arrow。但是当我点击关闭滑块时,图像保持为左侧。我对如何重写这个脚本来改变图像感到困惑。图像目前是分开的,一个朝向左侧,另一个朝下。我应该使用一个图像精灵,但我只是想先弄清楚这一点。

我读这篇文章,这是一个好主意,但也许有点太适合我复杂,现在实现:

Rotate image on toggle with jQuery

感谢。

编辑

我要指出,我在这里的问题页面比拨弄我发现有一点不同:

<div class="byline"> 
    <h4 class="crp-h">Analyze</h4> 
    <p class="crp-p">Things are written here</p> 
</div> 

<div class="toggle-content"> 
    <ul class="crp-list"> 
     <li>Statistical</li> 
     <li>Robust</li> 
    </ul> 
</div> 

感谢一招的小马......这件事几乎是除了工作一件事。只有当点击thisdiv时,旋转才起作用。当non-thisdiv点击其他div小号躲,但图像保持不旋转:

.byline{ 
    position:relative;  /* required */ 
}  


.byline:after { 
    transform: rotate(-90deg); 
    content: '';   /* required */ 
    position:absolute;  /* required */ 
    width: 14px;   /* required, width of your arrow */ 
    height: 14px;   /* required, height of your arrow */ 
    right: -20px;   /* required, negative width + some padding */ 
    top:0;     /* required */ 
    background: url('your/arrow/image/here.png') no-repeat; 
} 

.byline.exp:after { 
    transform: rotate(0deg); 
} 

新的jQuery的是在这里:

// expanding, collapsing div script 
$(document).ready(function() { 
    $(".toggle-content").hide(); 
    $(".byline").click(function() { 
     var $next = $(this).next(".toggle-content"); 
     $(".toggle-content").not($next).slideUp(); 
     $next.slideToggle(500); 
     $(this).toggleClass('exp'); 
    }); 
}); 

小提琴显示出这是在这里:

http://jsfiddle.net/nicorellius/7AYjj/

回答

1

将箭头图形放在伪元素上,如:afterand transform it,根据您的扳机连杆上切换一个类:

.byline:after{ 
    transform: rotate(-90deg); 
    /* arrow as background here */ 
} 

.byline.exp:after{ 
    transform: rotate(0deg); 
} 

在点击事件添加:

$(this).toggleClass('exp'); 
+0

我看到了小提琴的工作,这是有道理的。另外,当我实现它时,我的滑块/滑块工作正常,并且我在Firebug中看到班级'byline exp',所以我知道它有点工作......但是darn图像没有显示出来。我确认了我的样式表中的路径很好,并且图像位于服务器上...基于现有的格式,我没有使用您使用的“位置”值。这可能会导致我的问题? – nicorellius

+0

您是否添加了我的示例中的所有样式? ':在'要求'内容之后''''以及所有...... –

+0

是的,我确实......我只是编辑了我的问题。我的HTML与小提琴不太一样......我不熟悉后伪类,所以我想我会读一些......'content:'是我需要的唯一依赖吗? – nicorellius