【问题标题】:Rotate Arrow Image on Toggle Hide Div jQuery Script在切换隐藏 Div jQuery 脚本时旋转箭头图像
【发布时间】:2013-01-24 19:56:14
【问题描述】:

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

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", "");
    });
});

这可以将图像从左到下更改,例如,id 从空到down-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>

感谢 One Trick Pony……除了一件事,这件事几乎可以工作。旋转仅在单击 this div 时有效。当non-this div 被点击时,另一个divs 隐藏但图像保持未旋转:

.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/

【问题讨论】:

    标签: attr jquery


    【解决方案1】:

    根据您在触发器链接上切换的类,将箭头图形放在 :afterand transform it 等伪元素上:

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

    在您的点击事件中添加:

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

    【讨论】:

    • 我看到小提琴在工作,这是有道理的。另外,当我实现它时,我的滑块/隐藏器可以工作,并且我在 Firebug 中看到了 byline exp 类,所以我知道它有点工作......但是该死的图像没有出现。我确认我的样式表中的路径是好的,并且图像在服务器上...基于现有的格式,我没有使用您使用的 position 值。这会导致我的问题吗?
    • 您是否添加了我示例中的所有样式? :after 需要 content:'' 以及所有这些......
    • 是的,我做了...我刚刚对我的问题进行了编辑。我的 HTML 和小提琴不太一样……我对 after 伪类不熟悉,所以我想我会读一下……content:'' 是我在这里需要的唯一依赖项吗?跨度>
    • 你可以为箭头使用任何你想要的元素,它不必是:after,但它必须是一个新元素。 Here 你的 CSS 应该是这样的
    • 非常感谢 One Trick Pony!我的带有旋转箭头的可折叠divs 大部分都在工作。我肯定会接受你的回答,但我确实有另一个问题......如果我打开div,箭头会改变(好),当我点击this div 时,箭头会旋转回来,但随后如果我打开non-thisdiv,其他divs 被隐藏但图像保持未旋转。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    相关资源
    最近更新 更多