- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在元素上滑动时更改其不透明度。
我想实现一个类似于代码片段中的动画,它会根据我的手指/光标在滑动时拖动元素的程度逐渐应用。
编辑:动画与清除 Android 中的通知相同
我正在使用 ionic(我的元素是一个 ion-item),所以任何与 ionic/angular1 相关的东西都可以。
div.animated {
width: 100px;
height: 100px;
background: red;
position: absolute;
top: 31px;
animation: right 2s;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-timing-function: linear;
}
.back {
width: 100px;
height: 100px;
border: 1px solid blue;
position: fixed;
top: 30px;
left: 50px;
}
@keyframes right {
0% {
left: 0px;
opacity: 0.1;
}
50% { opacity: 1;}
100% {left: 100px;opacity:0.1}
}
The blue frame is the screen and the red square is the dragged element
<div class="animated"></div>
<div class="back"></div>
最佳答案
google chrome 开发人员非常友善的人进行了展示电话 SuperCharged该节目背后的想法是向您展示制作网络应用效果的快速简单方法。
他们做了一个episode (大约一个小时)关于可刷卡,他们快速进行了 10 分钟 episode只是为了给你基本的想法。
要回答您的问题,javascript 是使其响应的唯一方法,CSS 不会响应用户输入或操作。
此外,在屏幕上移动内容时,最好使用 transform,而不是 left。他们在展示期间非常详细地解释了原因,但快速的原因是转换可以使用 GPU。
无论如何,这是他们在剧集中制作的代码的现场演示,看一眼,看看它是否是您要找的。无论如何,我建议您观看他们的视频,您可以学到很多东西(我确实做到了)。
/**
Copyright 2016 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
*/
'use strict';
class Cards {
constructor() {
this.cards = Array.from(document.querySelectorAll('.card'));
this.onStart = this.onStart.bind(this);
this.onMove = this.onMove.bind(this);
this.onEnd = this.onEnd.bind(this);
this.update = this.update.bind(this);
this.targetBCR = null;
this.target = null;
this.startX = 0;
this.currentX = 0;
this.screenX = 0;
this.targetX = 0;
this.draggingCard = false;
this.addEventListeners();
requestAnimationFrame(this.update);
}
addEventListeners() {
document.addEventListener('touchstart', this.onStart);
document.addEventListener('touchmove', this.onMove);
document.addEventListener('touchend', this.onEnd);
document.addEventListener('mousedown', this.onStart);
document.addEventListener('mousemove', this.onMove);
document.addEventListener('mouseup', this.onEnd);
}
onStart(evt) {
if (this.target)
return;
if (!evt.target.classList.contains('card'))
return;
this.target = evt.target;
this.targetBCR = this.target.getBoundingClientRect();
this.startX = evt.pageX || evt.touches[0].pageX;
this.currentX = this.startX;
this.draggingCard = true;
this.target.style.willChange = 'transform';
evt.preventDefault();
}
onMove(evt) {
if (!this.target)
return;
this.currentX = evt.pageX || evt.touches[0].pageX;
}
onEnd(evt) {
if (!this.target)
return;
this.targetX = 0;
let screenX = this.currentX - this.startX;
if (Math.abs(screenX) > this.targetBCR.width * 0.35) {
this.targetX = (screenX > 0) ? this.targetBCR.width : -this.targetBCR.width;
}
this.draggingCard = false;
}
update() {
requestAnimationFrame(this.update);
if (!this.target)
return;
if (this.draggingCard) {
this.screenX = this.currentX - this.startX;
} else {
this.screenX += (this.targetX - this.screenX) / 4;
}
const normalizedDragDistance =
(Math.abs(this.screenX) / this.targetBCR.width);
const opacity = 1 - Math.pow(normalizedDragDistance, 3);
this.target.style.transform = `translateX(${this.screenX}px)`;
this.target.style.opacity = opacity;
// User has finished dragging.
if (this.draggingCard)
return;
const isNearlyAtStart = (Math.abs(this.screenX) < 0.1);
const isNearlyInvisible = (opacity < 0.01);
// If the card is nearly gone.
if (isNearlyInvisible) {
// Bail if there's no target or it's not attached to a parent anymore.
if (!this.target || !this.target.parentNode)
return;
this.target.parentNode.removeChild(this.target);
const targetIndex = this.cards.indexOf(this.target);
this.cards.splice(targetIndex, 1);
// Slide all the other cards.
this.animateOtherCardsIntoPosition(targetIndex);
} else if (isNearlyAtStart) {
this.resetTarget();
}
}
animateOtherCardsIntoPosition(startIndex) {
// If removed card was the last one, there is nothing to animate. Remove target.
if (startIndex === this.cards.length) {
this.resetTarget();
return;
}
const frames = [{
transform: `translateY(${this.targetBCR.height + 20}px)`
}, {
transform: 'none'
}];
const options = {
easing: 'cubic-bezier(0,0,0.31,1)',
duration: 150
};
const onAnimationComplete = () => this.resetTarget();
for (let i = startIndex; i < this.cards.length; i++) {
const card = this.cards[i];
// Move the card down then slide it up.
card
.animate(frames, options)
.addEventListener('finish', onAnimationComplete);
}
}
resetTarget() {
if (!this.target)
return;
this.target.style.willChange = 'initial';
this.target.style.transform = 'none';
this.target = null;
}
}
window.addEventListener('load', () => new Cards());
/**
Copyright 2016 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
*/
html,
body {
margin: 0;
padding: 0;
background: #FAFAFA;
font-family: Arial;
font-size: 30px;
color: #333;
}
* {
box-sizing: border-box;
}
.card-container {
width: 100%;
max-width: 450px;
padding: 16px;
margin: 0 auto;
}
.card {
background: #FFF;
border-radius: 3px;
box-shadow: 0 3px 4px rgba(0, 0, 0, 0.3);
margin: 20px 0;
height: 120px;
display: flex;
align-items: center;
justify-content: space-around;
cursor: pointer;
}
<!--
https://github.com/GoogleChrome/ui-element-samples/tree/master/swipeable-cards
https://www.youtube.com/watch?v=rBSY7BOYRo4
/**
*
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-->
<div class="card-container">
<div class="card">Das Surma</div>
<div class="card">Aerotwist</div>
<div class="card">Kinlanimus Maximus</div>
<div class="card">Addyoooooooooo</div>
<div class="card">Gaunty McGaunty Gaunt</div>
<div class="card">Jack Archibungle</div>
<div class="card">Sam "The Dutts" Dutton</div>
</div>
关于javascript - CSS/JS : Change opacity on swipe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38350116/
我在玩 CSS3 滤镜功能(模糊、对比、反转等),并注意到有一个 opacity 功能: filter: opacity(0.5); -webkit-filter: opacity(0.5); -mo
给定这个 html CSS #my_div { width: 100px; height: 100px;
我们大多数人都知道简单的 opacity CSS 规则,但最近我偶然发现了 filter,它可以将 opacity(amount) 作为它的值- 除其他事项外。但这两者到底有什么区别呢? 最佳答案 f
这个问题在这里已经有了答案: How do I reduce the opacity of an element's background using CSS? (29 个答案) 关闭 9 年前。
我想知道为什么设置元素的 Brush.Opacity 值比设置元素的 Opacity 属性更能提高性能。 来自 MSDN:Modifying an element's Opacity property
.item { opacity: 0; transition: opacity 6s; } 我正在使用 JS 将不透明度设置为 0 到 1,反之亦然。 CSS 中是否有一种方法可以使不透明度从
我想开始使用目前正在开发的 CSS3 filter:opacity() 和 filter:drop-shadow() 属性,因为我读到它们在某些机器上的某些浏览器中是硬件加速的。但在这个早期阶段,我确
在QML中,如何防止子元素继承其父元素的不透明度? 我想为父元素和子元素设置不同的不透明度值。 最佳答案 实际上,为父元素设置layer.enabled: true对我有用。 将整个元素呈现到缓冲区,
我想要有阴影/发光的文本,但问题是文本本身是稍微透明的,阴影从它后面显示出来,给文本着色。 color: rgba(226,229,226,0.88); -webkit-text-stroke-wid
是否可以只调整文本阴影的不透明度,而不是调整文本本身的不透明度? 例如,我有带有蓝色阴影的紫色文本。我想让蓝色阴影具有不透明度,而不会在紫色上丢失任何东西。 h1.blue {text-shadow:
.imgopacity{ opacity:0.2px; } 在上面的CSS代码中,不支持opacity属性,如果我想拥有opacity属性,如何获取。 最佳答案 Opacity 属性不是以
我希望我的 HTML 背景不透明度为 .9,但由于某些原因它不起作用。为什么会这样我真的不明白... CSS html{ background: url(http://img.pixland.uz
我有一个问题。 我正在尝试用纯 CSS 重新创建这个 gif: http://i.imgur.com/VrV57jC.gif 所以,这是代码示例 http://jsfiddle.net/E4WMh/
我知道您可以在 Vim 中使背景色透明,但您也可以降低颜色的不透明度吗? 我试过这样做但没有成功: highlight Normal ctermbg=000000CC highlight NonTex
因为 WPF 没有允许显示多个月份的月历,所以我尝试在 WindowsFormsHost 中使用经典的 WindowsForms MonthCalendar。在“普通”WPF 窗口中执行此操作效果很好
我正在尝试对图层进行动画处理,以便不透明度在大约 0.2 秒内从 0 变为 1,将不透明度保持在 1 秒,然后将不透明度放回 0。我正在尝试使用关键时间去做,但我做不到。 CAKeyframeAnim
我使用 Javascript 和 Mootools 创建一个特定的 HTML 页面,其中包括一个 javascript 文件,用于检查元素的不透明度以进行特定操作。 但是,在 IE 上,我遇到了 el
我想设置 LinearLayout 背景的不透明度,我找到了这个 solution如果我使用普通布局,它工作正常,但在 android-widgets 的情况下,我们必须设置不同的布局我写了以下代码
我需要更改 DIV 的不透明度(如果有所不同,可能包含图像)。通常人们会使用 $('#id1').css('opacity', opacity1); $('#id2').css('opacity',
所以我想在我的网站上制作一个按钮,当悬停时将使页面内容的不透明度为 0。这是我的代码 #wrapper{ box-shadow:0 0 15px rgba(0,0,0,1); position:rel
我是一名优秀的程序员,十分优秀!