javascript - 如何平滑地更改背景颜色?

标签 javascript jquery html css

这是有问题的代码 - https://codepen.io/illpill/pen/VbeVEq

function newColor() {
    var randomNumber = Math.floor(Math.random() * (colors.length));
    document.body.style.backgroundColor = colors[randomNumber];
}

现在是 newColor() 函数...

当点击按钮时,我希望它能平滑地过渡到下一个引用而不是突然加载它。我还希望报价逐渐变成白色,然后变成新的报价...这是如何实现的?使用 jQuery 更好吗?

谢谢!

最佳答案

background-color 属性添加过渡到你的 body 。

body {
  transition : background-color 1s ease-in-out;
}

要将过渡添加到您应该编写的任何元素,

element-selector {
  transition: [CSS property] [duration] [timing-function];
}

对于标题标签,它将是

h3,h4 {
  transition: [css property] [duration] [timing-function]
}

Transition only works when there is a change in the CSS property specified for the given element.

关于javascript - 如何平滑地更改背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43508845/

相关文章:

javascript - 如何使用 jquery 创建带导航按钮的幻灯片

javascript - 如何在每次函数调用前等待 setTimeout?

javascript - 根据 JSON 键的数量添加 html 元素

javascript - 使用 div 作为链接 - 打开新选项卡的选项?

javascript - 使用 Rangy Library 在 contentEditable div 中获取准确的插入符位置

php - 发布多个动态创建的输入并将它们插入 MySQL 数据库

上传文件 <input> 的 php javascript 表单验证

javascript - 等待直到对象被创建

javascript - 如何在 JS 执行后延迟 CSS 和脚本加载

javascript - 如何用旋转背景图像填充浏览器窗口?