我window.scrollBy()用于平滑滚动,但它不适用于 Safari。修复它的最佳方法是什么?
function scroll(id: number) {
for (let k = 0; k < sectionsForScroll.length; k++) {
if (sectionsForScroll[k].dataset.navId == id) {
const bigginer = sectionsForScroll[0].getBoundingClientRect().top;
console.log(bigginer + 'px how far we from the start ');
const distanceToGo = sectionsForScroll[k].getBoundingClientRect().top;
console.log(sectionsForScroll[k].offsetTop);
const distanceToScroll = bigginer - distanceToGo;
console.log(distanceToGo + ' where we have to go ');
console.log(distanceToScroll + ' what the distanse we need to scroll ');
window.scrollBy({left:0, top:distanceToGo, behavior:"smooth"});
}
}
}
Run Code Online (Sandbox Code Playgroud) 我想计算一下我的工作时间.我输入时工作正常
08:00 - 09:00 = 01:00
Run Code Online (Sandbox Code Playgroud)
但是当我输入这个时间
23:30 - 01:30 = 10:00
Run Code Online (Sandbox Code Playgroud)
它应该回来 02:00
function pad(num) {
return ("0" + num).slice(-2);
}
function diffTime(start, end) {
var s = start.split(":"),
sMin = +s[1] + s[0] * 60,
e = end.split(":"),
eMin = +e[1] + e[0] * 60,
diff = eMin - sMin;
if (diff < 0) {
sMin -= 12 * 60;
diff = eMin - sMin
}
var h = Math.floor(diff / 60),
m = diff % 60;
return …Run Code Online (Sandbox Code Playgroud)正在编写一个函数,该函数接受一个数字数组,如果缺少数字,则返回true和索引;如果没有数字,则返回false。我只是注意到关于数组的某些东西使我感到困惑。
像这样的数组
[,1,2,3,4]
Run Code Online (Sandbox Code Playgroud)
将打印
[undefined,1,2,3,4]
Run Code Online (Sandbox Code Playgroud)
数组以逗号开头,输出对我有意义
但是为什么
[1,2,3,4,] // Notice that the array ends with a comma
Run Code Online (Sandbox Code Playgroud)
打印
[1,2,3,4]
Run Code Online (Sandbox Code Playgroud)
我本来以为输出是[1,2,3,4,undefined]。
有谁知道为什么会这样吗?
我无法导出默认的{多个功能}来工作。
这是我的设置:
src/index.js:
import {foo} from './foo'
foo();
Run Code Online (Sandbox Code Playgroud)
foo.js:
const foo = () => {
console.log("Hello!");
}
export default {foo};
Run Code Online (Sandbox Code Playgroud)
和我的package.json:
{
"name": "jestjs.io",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "babel-node src/index.js",
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.4.5",
"@babel/node": "^7.4.5",
"@babel/preset-env": "^7.4.5",
"babel-jest": "^24.8.0",
"jest": "^24.8.0",
"nodemon": "^1.19.1"
}
}
Run Code Online (Sandbox Code Playgroud)
运行时npm start,出现以下错误消息:
/src/index.js:5
(0, _foo.foo)();
^
TypeError: (0 , _foo.foo) is not a function …Run Code Online (Sandbox Code Playgroud) 有时我只需要断言实际对象的特定属性,而不关心其他属性。
例如:
const actual = [
{ a: 1, additionalProp: 'not interestig' },
{ a: 2, additionalProp: 'not interestig' }
];
expect(actual).toEqual([
{ a: 1 },
{ a: 2 }
])
Run Code Online (Sandbox Code Playgroud)
目前失败的原因是:
预计 $[0] 不具有属性
additionalProp:'随机值'
怎样写出期望呢?
我目前正在做这样的事情:
expect(actual.map(i => ({a: 1}))).toEqual([
{ a: 1 },
{ a: 2 }
])
Run Code Online (Sandbox Code Playgroud)
但我不太喜欢它并且它不适用于更复杂的对象
我刚刚注意到,在 Chrome 中不再需要选择具有 id 的元素,例如 with getElementById,您可以直接使用其名称调用该元素id...例如"para1"在此示例中:
<head>
<title>getElementById Beispiel</title>
<script>
function changeColor(newColor) {
para1.style.color = newColor;
}
</script>
</head>
<body>
<p id="para1">Irgendein Text</p>
<button onclick="changeColor('blue');">Blau</button>
<button onclick="changeColor('red');">Rot</button>
</body>Run Code Online (Sandbox Code Playgroud)
这方面还有更多规范吗?在互联网上没有找到任何有关它的信息..
我的要求是使用Plotly.js在X轴的某些日期绘制垂直线。我是新手,我设法找到了一个使用“形状”选项绘制垂直线的选项。
shapes: [{
type: 'line',
x0: '2000-01-11',
y0: 0,
x1: '2000-01-11',
y1: 7,
line: {
color: 'grey',
width: 1.5,
dash: 'dot'
}
}]
Run Code Online (Sandbox Code Playgroud)
但是我面临的主要困难是绘制垂直线时如何定义Y轴值,因为Y轴比例会根据数据而有所不同。我希望它是动态的像y0:0; y1:d3中的(height-margin.top-margin.bottom),我们用来画一条垂直线。
我有这个数组,它被格式化为字符串:
['6.35', '2.72', '11.79', '183.25']
Run Code Online (Sandbox Code Playgroud)
问题是当我将其转换为数字时(使用 - 不带双引号)
array.match(/\d+/g).map(Number) || 0;
它将用于小数的点更改为逗号。然后我最终得到这个新数组:
6,35,2,72,11,79,183,25
Run Code Online (Sandbox Code Playgroud)
因此,数组中不再有 4 个项目,现在我有 8 个项目,因为我的分隔符是逗号。
关于如何在不替换点的情况下转换此数组的任何想法?
我正在尝试创建一个在新行上打印每个单词的函数。给定的参数是一个字符串,该字符串的第一个单词(即“ helloMyNameIsMark”)除外,但不以空格分隔。我有一些可行的方法,但想知道在javaScript中是否有更好的方法可以做到这一点。
separateWords = (string) => {
const letters = string.split('');
let word = "";
const words = letters.reduce((acc, letter, idx) => {
if (letter === letter.toUpperCase()) {
acc.push(word);
word = "";
word = word.concat(letter);
} else if (idx === letters.length - 1) {
word = word.concat(letter);
acc.push(word);
} else {
word = word.concat(letter)
}
return acc
}, []);
words.forEach(word => {
console.log(word)
})
}
Run Code Online (Sandbox Code Playgroud) 使用此代码获取ymd:
var ymd = '20190716';
var year = ymd.substring(0, 4);
var month = ymd.substring(4, 2);
var day = ymd.substring(6, 2);
console.log(year);
console.log(month);
console.log(day);Run Code Online (Sandbox Code Playgroud)
我认为从索引到长度获取子字符串的正确方法。所以...
想:
2019
07
16
Run Code Online (Sandbox Code Playgroud)
但是得到了:
2019
19
1907
Run Code Online (Sandbox Code Playgroud)
为什么?