css怎样调用css函数_8个具有CSS函数的巧妙技巧

css怎样调用css函数

CSS is capable of much more than many web developers realise. The stylesheet language continues to get more and more powerful with every passing year, bringing functionality to the browser that otherwise would be fulfilled by JavaScript. In this article, we’ll take a look at eight clever tricks with CSS functions that do not require JavaScript at all.

CSS的功能远远超过许多Web开发人员意识到的。 样式表语言每年都在变得越来越强大,为浏览器带来了JavaScript否则无法实现的功能。 在本文中,我们将介绍使用CSS函数的8个技巧,这些技巧根本不需要JavaScript。

1.纯CSS工具提示 (1. Pure CSS Tooltips)

A lot of websites still use JavaScript to create tooltips but it is actually much easier with CSS. The simplest solution is to provide the tooltip text in a data attribute in your HTML code, e.g. data-tooltip="…". With that in your markup, you can then place the following into your CSS to display the tooltip text inside the attr() function:

许多网站仍然使用JavaScript来创建工具提示,但是使用CSS实际上要容易得多。 最简单的解决方案是在HTML代码的data属性中提供工具提示文本,例如data-tooltip="…" 。 使用标记中的内容,然后可以将以下内容放入CSS中,以在attr()函数内显示工具提示文本:

.tooltip::after {
  content: attr(data-tooltip);
}

Quite self-explanatory, right? Of course, there is more code needed to actually style the tooltips but fear not, there is an awesome, pure CSS library for this exact purpose called Hint.css.

不言自明,对吗? 当然,实际上需要更多的代码来设置工具提示的样式,但不要担心,为此目的,有一个很棒的纯CSS库称为Hint.css

2.(Ab)使用自定义数据属性和attr()函数 (2. (Ab)using Custom Data Attributes and the attr() Function)

We already used attr() for tooltips but there are some other use cases for it too. In combination with data attributes, you can build a thumbnail image with title and description using just one single line of HTML code:

我们已经使用了attr()作为工具提示,但是也有其他一些用例。 结合数据属性,您可以仅使用一行HTML代码来构建带有标题和描述的缩略图:

<a class="caption" href="#" data-title="Vulture" data-description="...">
  <img src="img.jpg" alt="Illustration"/>
</a>

Now you can use the attr() function to display the title and description:

现在,您可以使用attr()函数显示标题和描述:

.caption::after {
  content: attr(data-title);
    ...
}

Here’s a working example of a thumbnail with animated captions that show on hover:

这是一个带有悬停显示的动画字幕的缩略图的工作示例:

See the Pen Thumbnail with Animated Captions by SitePoint (@SitePoint) on CodePen.

请参见CodePen带有 SitePoint( @SitePoint ) 动画字幕的钢笔缩略图

Note: Generated content could be problematic in terms of accessibility. This article on accessibility support for CSS generated content is a useful read on that very topic.

注意: 就可访问性而言,生成的内容可能会出现问题。 关于CSS生成的内容的可访问性支持的这篇文章对于该主题非常有用。

3. CSS计数器 (3. CSS Counters)

What you can do with CSS counters seems like magic. It is not the most well-known feature and most people probably would guess that it is not so well supported but actually, every browser supports CSS counters:

您可以使用CSS计数器执行的操作似乎很神奇。 它不是最著名的功能,大多数人可能会猜测它的支持程度不是很好,但是实际上,每个浏览器都支持CSS计数器:

See Can I Use css-counters? Data on support for the css-counters feature across the major browsers from caniuse.com.

请参阅我可以使用css计数器吗? 来自caniuse.com的主要浏览器上有关css-counters功能的支持数据。

While you should not use CSS counters for ordered lists (<ol>), counters are great for things like pagination or displaying numbers underneath items in a gallery. You can also count the number of checked items which is quite impressing given how little code you need (and no JavaScript):

尽管您不应该将CSS计数器用于有序列表( <ol> ),但是计数器非常适合用于分页或在图库中项目下方显示数字。 您还可以计算已检查项目的数量,这给您留下了深刻的印象,因为您需要的代码很少(并且没有JavaScript):

See the Pen Selection CSS Counter by Will Boyd (@lonekorean) on CodePen.

见笔选择CSS计数器由威尔·博伊德( @lonekorean )上CodePen

CSS counters are also great for dynamically changing numbers in a list of items which can be reordered by dragging and dropping:

CSS计数器对于动态更改项目列表中的数字也非常有用,这些项目可以通过拖放来重新排序:

See the Pen CSS Counters drag-and-drop demonstration by SitePoint (@SitePoint) on CodePen.

请参阅CodePenSitePoint ( @SitePoint )进行的Pen CSS Counters拖放演示

Just as with the last example, remember — generated content could be problematic in terms of accessibility.

与上一个示例一样,请记住, 就可访问性而言,生成的内容可能会出现问题。

4.带有CSS过滤器的磨砂玻璃 (4. Frosted Glass with CSS Filters)

With iOS 7, Apple brought us the “frosted glass” effect — translucent, blurry elements that look like a frosted glass window. This effect is starting to appear in many of places, inspired by Apple’s implementation. Recreating this effect is a bit tricky — before there were CSS filters, you had to create the frosted glass effect with blurred images. Now that CSS filters are fully supported by almost all major browsers, it is much easier to recreate this effect.

在iOS 7中,Apple为我们带来了“毛玻璃”效果-半透明,模糊的元素,看起来像毛玻璃窗口。 受苹果实施的启发,这种效果已开始在许多地方出现。 重新创建此效果有些棘手-在使用CSS滤镜之前,您必须创建带有模糊图像的磨砂玻璃效果。 既然几乎所有主流浏览器都完全支持CSS过滤器, 那么重新创建此效果要容易得多

See Can I Use css-filters? Data on support for the css-filters feature across the major browsers from caniuse.com.

请参阅我可以使用css-filters吗? 来自caniuse.com的跨主要浏览器的css-filters功能支持数据。

In the future, we could create similar effects using backdrop filters and the filter() function which are both currently only supported by Safari.

将来,我们可以使用背景滤镜和filter()函数创建类似的效果,而这两种滤镜目前仅受Safari支持。

5.使用HTML元素作为背景图像 (5. Using HTML Elements as Background Images)

Normally, you would specify a JPEG or PNG file as background image or maybe a gradient. But did you know that with the element() function, you can also use a <div> as a background image? Currently, the element() function is only supported by Firefox:

通常,您可以将JPEG或PNG文件指定为背景图像或渐变。 但是您知道吗,通过element()函数,您还可以将<div>用作背景图像吗? 当前,仅Firefox支持element()函数:

See Can I Use css-element-function? Data on support for the css-element-function feature across the major browsers from caniuse.com.

请参阅我可以使用css-element-function吗? 来自caniuse.com的跨主要浏览器的css-element-function功能支持的数据。

The possibilities are nearly endless, here is one example from MDN.

可能性几乎是无限的, 这里是 MDN的一个例子

6.带有calc()智能网格 (6. Smarter Grids with calc())

Fluid grids are a great thing but there are some serious problems. For instance, it is impossible to have a gutter on the top and bottom that is the same size as a gutter on the left and right. Also, the markup is really messed up, depending on which grid system you use. Even flexbox alone is not the ultimate solution but with the calc() function which can be used as a value in CSS, grids can become much better. In this tutorial here at SitePoint, George Martsoukos shows some practical examples, such as a gallery grid with perfect spacing. Using CSS preprocessors such as Sass, putting together a creative grid system can be incredibly simple and easy to maintain. With browser support also being nearly perfect, calc() is a handy feature you should definitely use.

流体网格是一件好事,但存在一些严重的问题。 例如,不可能在顶部和底部设置与左侧和右侧的排水槽相同大小的排水槽。 另外,根据您使用的网格系统,标记确实很混乱。 即使单独使用flexbox也不是最终的解决方案,但是使用可以在CSS中用作值的calc()函数,网格可以变得更好。 在此SitePoint的教程中 ,George Martsoukos展示了一些实际示例,例如具有完美间距的画廊网格。 使用诸如Sass之类CSS预处理器, 将创意网格系统组合在一起可以非常简单且易于维护。 由于浏览器支持也将近完美, calc()是您绝对应该使用的便捷功能。

See Can I Use calc? Data on support for the calc feature across the major browsers from caniuse.com.

请参阅我可以使用计算器吗? 来自caniuse.com的跨主要浏览器的calc功能支持数据。

7.对齐position:fixed元素与CSS calc() (7. Aligning position:fixed Elements with CSS calc())

Another use case for the calc() function is aligning elements with a fixed position. For example, if you have a content wrapper with fluid spacing to the left and right, and you want to precisely align a fixed element inside of that wrapper — you’re going to have a hard time figuring out which value to choose for the “right” or “left” property. With calc(), you can combine relative and absolute values to perfectly align the element:

calc()函数的另一个用例是将元素与固定位置对齐。 例如,如果您有一个内容包装容器,该包装容器的左右方向之间有一定的间距,并且您想在该包装容器内精确地对齐固定元素,那么您将很难确定要为“右”或“左”属性。 使用calc() ,您可以结合相对和绝对值来完美对齐元素:

.wrapper {
  max-width: 1060px;
  margin: 0 auto;
}
.floating-bubble {
  position: fixed;
  right: calc(50% - 530px); /* 50% - half your wrapper width */
}

Here’s an example:

这是一个例子:

See the Pen Aligning “position: fixed” elements with CSS calc() by SitePoint (@SitePoint) on CodePen.

请参阅CodePen上的SitePoint ( @SitePoint ) 带有CSS calc()的笔对齐“位置:固定”元素

8.带有cubic-bezier()动画 (8. Animations with cubic-bezier())

To make the UI of a website or app more attractive, you can use animations but the standard easing options are pretty limited. For example, "linear" or "ease-in-out". Things like bouncy animations aren’t even possible with the standard options. With the cubic-bezier() function, you can animate elements exactly the way you want.

为了使网站或应用程序的用户界面更具吸引力,您可以使用动画,但是标准的缓动选项非常有限。 例如, "linear""ease-in-out" 。 使用标准选项甚至无法实现弹性动画。 使用cubic-bezier()函数,可以完全按照所需的方式对元素进行动画处理。

There are two ways to use cubic-bezier() — understanding the mathematics behind it and building it yourself, or using a cubic-bezier generator.

有两种使用cubic-bezier()了解其背后的数学并自己构建,或者使用三次bezier生成器

Honestly, I’d go with the latter.

老实说,我会选择后者。

结论 (Conclusion)

A clever use of CSS functions not only solves known problems like establishing smarter grid systems but it also gives you more creative freedom. With browser support getting better and better, you should really take a critical look at your CSS and improve it with functions like calc().

巧妙地使用CSS函数不仅可以解决已知的问题,例如建立更智能的网格系统,还可以为您提供更多的创作自由。 随着对浏览器的支持越来越好,您应该认真看待CSS并使用calc()类的功能对其进行改进。

翻译自: https://www.sitepoint.com/8-clever-tricks-with-css-functions/

css怎样调用css函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值