简体   繁体   English

使用JavaScript更改项目符号图片的大小?

[英]Change size of bullet image using Javascript?

I loaded an image bullet style via a url: 我通过网址加载了图像项目符号样式:

var a = document.createElement('li');
a.style.listStyleImage = "url(some url)";
a.style.listStylePosition = "inside";

But I want to increase the size of the bullet (image) using Javascript, is that possible? 但是我想使用Javascript增加项目符号(图像)的大小,这可能吗?

As was already mentioned in the comments, list-style-image does not support the behaviour you want. 正如评论中已经提到的那样, list-style-image不支持您想要的行为。

Using backgrounds 使用背景

If you don't have a background already : Just use the elements background-image property isntead. 如果您还没有背景 :只需使用元素background-image属性istead。

If you do have a background already : In CSS3 it's possible to define multiple backgrounds. 如果您已经有背景 :在CSS3中,可以定义多个背景。 This means that you can use the bullet image as an additional non repeating background as well giving you full control over it. 这意味着您可以将项目符号图像用作附加的非重复背景,也可以完全控制它。 Controlling multiple backgrounds though javascript is not a lot of fun though and I haven't heard of any good plugins for it either. 尽管控制javascript并没有多大乐趣,但是我也没有听说过任何适合它的插件。 You might want to take a look around though, someone might have made something and otherwise just write your own abstraction layer. 但是,您可能想看看周围,有人可能已经做了一些事情,否则只需编写您自己的抽象层即可。

Using data url's 使用数据网址

An alternative, more crazy, approach would be to load the image through ajax, draw it onto an canvas scaled in any way you like, generate a data uri from the canvas and set that as the uri of the list-style-image property. 另一种更疯狂的方法是通过ajax加载图像,以任意方式将其绘制到画布上,从画布生成数据uri,并将其设置为list-style-image属性的uri。

Use ::before pseudo class to display bullet 使用::before伪类显示项目符号

HTML: HTML:

<ul class="list">
    <li class="list-item">test
    </li>
    <li class="list-item">test
    </li>
<ul>

CSS: CSS:

.list{
    list-style: none;
}

.list-item::before{
    content: "";
    width: 30px;
    height: 30px;
    display: inline-block;
    background-image: url(http://placekitten.com/20/30);
    background-size: 200%;
}

http://jsfiddle.net/PtG6Q/1/ http://jsfiddle.net/PtG6Q/1/

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

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