Table of Contents
元素跟随鼠标移动
页面滚动到一定距离时,元素发生变化
经过不懈的努力取得的结果和别人通过关系取得的结果一样甚至更差,那努力还有什么意义呢?
本文示例 Source Code:
Home Web Front-end HTML Tutorial dynamic-css 动态 CSS 库,使得你可以借助 MVVM 模式动态生成和更新 css,从 js 事件和 css 选择器的苦海中脱离出来_html/css_WEB-ITnose

dynamic-css 动态 CSS 库,使得你可以借助 MVVM 模式动态生成和更新 css,从 js 事件和 css 选择器的苦海中脱离出来_html/css_WEB-ITnose

Jun 24, 2016 am 11:31 AM

dynamic-css 使得你可以借助 MVVM 模式动态生成和更新 css,从而将本插件到来之前,打散、嵌套在 js 中的修改样式的代码剥离出来。比如你要做元素跟随鼠标移动,或者根据滚动条位置的变化而触发一些效果,原本你要写 js 去绑定滚动事件,然后计算偏移量,然后更新元素 css,使用 dynamic-css,你只要根据语法去写好 css 表达式就可以了。对于一些原本需要复杂的 js 判断的动态 css,用 dynamic css 表达式几行代码搞定。

 

举两个例子:

元素跟随鼠标移动

实现它的源代码:

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title>元素跟随鼠标移动 - DynamicCss Demo</title>    <meta http-equiv="X-UA-Compatible" content="IE=11,chrome=1" />    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <script src="jquery.js"></script>    <script src="avalon.shim.js"></script>    <script src="avalon.dynamiccss.js"></script>    <script src="layout.js"></script></head><body>    <strong><style type="text/dynamiccss" ms-controller="layout" ms-dynamiccss> .mouse-follow { position: absolute; top:{{layout.mouse.pageY}}px; left:{{layout.mouse.pageX}}px; } </style></strong>    <div class="<strong>mouse-follow</strong>">为什么追我</div></body></html>
Copy after login

页面滚动到一定距离时,元素发生变化

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title>页面滚动到一定距离时,元素发生变 - DynamicCss Demo化</title>    <meta http-equiv="X-UA-Compatible" content="IE=11,chrome=1" />    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <script src="jquery.js"></script>    <script src="avalon.shim.js"></script>    <script src="avalon.dynamiccss.js"></script>    <script src="layout.js"></script></head><body>    <style>        html { width: 640px;margin:0 auto; }        .content-sum {            display: none;            position: fixed;            width:640px;            top: 0;            background: #808080;            height:50px;            line-height:50px;            text-align:center;        }       .content-full{            height:9000px;        }    </style>    <strong><style type="text/dynamiccss" ms-controller="layout" ms-dynamiccss> .content-sum { display:{{layout.doc.scrollTop > 135 ? "block" : "none"}}; /* 这里面可以写 js 代码,可以调用 js 方法,基本无限制 */ } </style></strong>    <b>往下滚动试试</b>    <div class="big-box">        <div class="<strong>content-sum</strong>">经过不懈的努力取得的结果和别人通过关系取得的结果一样甚至更差,那努力还有什么..</div>        <h1 id="经过不懈的努力取得的结果和别人通过关系取得的结果一样甚至更差-那努力还有什么意义呢">经过不懈的努力取得的结果和别人通过关系取得的结果一样甚至更差,那努力还有什么意义呢?</h1>        <div class="content-full">            考研过,失败过,后来也成功过。<br /><br />            去考研论坛围观过连续7战的奇葩,围观过为了考研抛弃女友、抛弃人际关系奋力一战的汉子,围观过被考研折磨的精神失常的病人,也认识大学玩四年,考研复习两个月既考上985(理工科考研,非文科)院校天赋灵异的奇才            .....        </div>    </div></body></html>
Copy after login

 

如果是用 js 来实现这些效果的话,就会充斥着各种事件,各种变量的加加减减,和 css 选择器更新,代码挺丑的,而且和 dom 类似,情况复杂之后 js 事件相互触发稍有不慎就会坠入苦海。

dynamic-css 是基于 avalon js 的一个扩展,只要是 avalon js 支持的语法,dynamic-css 都能够支持,甚至可以在 css 表达式里面调用 js function。

dynamic-css 使你从此脱离事件和选择器的苦海,来到数据和绑定的乐园!欢迎使用和交流!

本文示例 Source Code:

https://github.com/darklx/dynamic-css

dynamic-css 使用了 jQuery,但并不是必须依赖 jQuery ,只是用了 jQuery 的 $.get 方法,如果你的项目不想引入 jQuery,也是可以的,传给 dynamic-css 具有相同 get 方法功能的对象即可。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

See all articles