简体   繁体   English

如何为列表项的悬停和非悬停行为动态设置背景图像 url?

[英]How to set background image url both for hover and non-hover behaviors of a list item dynamically?

I have a requirement where in the user can create new list items inspite of having default list items.我有一个要求,尽管具有默认列表项,但用户可以在其中创建新列表项。 So The user will be allowed to attach two icon images for the list items to show on hover of list item and on non hover of list item.因此,用户将被允许为列表项附加两个图标图像,以在列表项悬停和非悬停列表项时显示。 So, now I want to change the background image to the given URL's in the property of the object that is iterated in ng-repeat.所以,现在我想将背景图像更改为在 ng-repeat 中迭代的对象属性中的给定 URL。 I have been trying using jquery to get the property and its value but couldn't replace with the hover_image URL from the object on hover.我一直在尝试使用 jquery 来获取属性及其值,但无法用悬停时对象中的 hover_image URL 替换。

<ul id="user_events_list" class="listing_categories" ng-repeat="event in privateEventItems">
    <li class="eventListItem" style="background-image :url('{{event.eventIconUrl}}'); background-position:10px; background-repeat : no-repeat; padding-left:30px;">{{event.eventName}}
    </li>
</ul>

I want to change the {{event.eventIconUrl}} to {{event.eventHoverIconUrl}} on mouse hover on the list item.我想改变{{event.eventIconUrl}}{{event.eventHoverIconUrl}}鼠标悬停在列表项。 Can anyone help me out with this?谁能帮我解决这个问题?

You may use ng-mouseenter and ng-mouseleave events.您可以使用ng-mouseenterng-mouseleave事件。

<ul id="user_events_list" class="listing_categories" ng-repeat="event in privateEventItems">
    <li class="eventListItem" ng-init="anyChosenVariableNameForUrls=event.eventIconUrl"
        style="background-image :url('{{anyChosenVariableNameForUrls}}'); background-position:10px; background-repeat : no-repeat; padding-left:30px;"
        ng-mouseenter="anyChosenVariableNameForUrls=event.eventHoverIconUrl"
        ng-mouseleave="anyChosenVariableNameForUrls=event.eventIconUrl"
    >{{event.eventName}}
    </li>
</ul>

Try this:尝试这个:

 $('#name img').hover(function() { $(this).attr('src', 'https://developers.google.com/maps/images/lhimages/api/icon_placesapi.svg'); }, function() { $(this).attr('src', 'http://icons.iconarchive.com/icons/fasticon/web-2/256/Google-icon.png'); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#" id="name"> <img title="Hello" src="http://icons.iconarchive.com/icons/fasticon/web-2/256/Google-icon.png" /> </a>

To do it in angular way see this Fiddle要以角度方式执行此操作,请参阅此Fiddle

use ng-mouseover and ng-mouseleave directives to detect mouse hover and leave.使用ng-mouseoverng-mouseleave指令来检测鼠标悬停和离开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM