- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个类似于 before-after.js 或 cocoen 的之前/之后图像 slider 作为 Rails 的自定义 polymer Web 组件。但是,我的实现遇到了一些 JavaScript 问题。在这个问题的过程中,有些已经得到了解决。剩下的主要问题是:
window.onload
内位于组件 .html 的最末尾。以下是 slider 的 HTML、JS 和 CSS 文件:
image-slider.html:
<dom-module id="image-slider">
<link rel="stylesheet" href="image-slider.css" />
<template>
<div id="dual-wrapper" style$="border: [[border]];
border-radius: [[border_radius]]; width: [[width]];
height: [[height]]; margin: [[margin]];">
<div id="img-snd-wrap">
<img src$="[[snd]]" class="img-snd">
</div>
<div id="img-fst-wrap">
<img src$="[[fst]]" class="img-fst">
</div>
<div class="img-blind" style="width: [[width]]; height: [[height]]"></div>
<div id="img-transition-slider" style$="height: [[height]];">
<div id="img-transition-slider-handle"
style$="margin-top: calc([[height]]/2 - [[handle_height]]/2);
height: [[handle_height]];">
</div>
</div>
</div>
</template>
<script src="image-slider.js"></script>
</dom-module>
image-slider.js:
Polymer({
is: "image-slider",
properties: {
fst: {
type: String
},
snd: {
type: String
},
width: {
type: String
},
height: {
type: String
},
border: {
type: String,
value: "none"
},
border_radius: {
type: String,
value: "0px"
},
handle_height: {
type: String,
value: "80px"
}
},
attached: function () {
var slider, first, second, container, x, prev_x, containerWidth;
slider = this.$['img-transition-slider'];
console.log(slider);
first = this.$['img-fst-wrap'];
second = this.$['img-snd-wrap'];
container = this.$['dual-wrapper'];
slider.onmousedown = function(e) {
document.body.style.cursor = "col-resize";
containerWidth = container.clientWidth;
prev_x = x - slider.offsetLeft;
slider.querySelector("#img-transition-slider-handle").style["background-color"] = '#888';
};
document.onmousemove = function(e) {
// X coordinate based on page, not viewport.
if (e.pageX) { x = e.pageX; }
// If the object specifically is selected, then move it to
// the X/Y coordinates that are always being tracked.
if(slider) {
var toReposition = (x - prev_x);
var newPosition = ((toReposition > containerWidth) ?
containerWidth - 2
: ((toReposition < 0) ?
0
:
toReposition
));
slider.style["margin-left"] = newPosition + 'px';
second.style["width"] = newPosition + "px";
first.style["width"] = (containerWidth - newPosition) + "px";
first.style["margin-left"] = newPosition + "px";
first.getElementsByTagName("img")[0].style["margin-left"] = (-newPosition) + "px";
}
};
document.onmouseup = function() {
document.body.style.cursor = "default";
slider.querySelector("#img-transition-slider-handle").style["background-color"] = '#555';
slider = false;
};
}
});
image-slider.css:
:host {
display: block;
}
#dual-wrapper {
display: block;
overflow: hidden;
position: relative;
-moz-user-select: -moz-none;
-khtml-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
}
#img-fst-wrap, #img-snd-wrap {
display: block;
position: absolute;
overflow: hidden;
}
.img-blind {
display: block;
position: absolute;
}
#img-transition-slider {
position: absolute;
display: block;
width: 0px;
border: 1px solid #333;
border-top: none;
border-bottom: none;
}
#img-transition-slider:hover {
cursor: col-resize;
}
#img-transition-slider-handle {
width: 10px;
margin-left: -5px;
background-color: #555;
border-radius: 2px;
-webkit-transition: background-color .3s;
transition: background-color .3s;
}
最佳答案
如果 Web 组件不可复制,那是因为您对本身重复的 id
使用了 document.getElementById()
。因此,只有使用此 id
定义的第一个(或最后一个)元素将始终被返回。
您应该使用this.$.[sub-element's id]
选择 Polymer 元素子树内的一个元素,并从 Polymer 元素内部添加鼠标事件监听器,位于 attached() callback
中方法:
var slider = this.$['image-transition-slider']
//define the mouse event listeners inside the element
其中 this
是对自定义元素本身的引用。
关于javascript - polymer - slider 前/后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39648569/
我有一个Polymer元素,并且在它的Javascript中,我试图找到它的宽度和高度,就像它在DOM中一样。我尝试了很多方法,但总是得到0。 最佳答案 ...这是对我有用的一个: 将Polymer.
如何在“polymer 3”元素中使用“polymer 2”元素?由于元素在shadow dom内部不起作用,因此以下内容不起作用。 static get template() { retur
在我的 React 项目中,我使用 ES6 模块已有一段时间了。在我的 React 组件中,我将使用 import: import {f1,f2} from "./myLib"; 在我的 polyme
我想知道是否有用于聚合物元素的CDN,因为您必须始终下载元素,并且通过CDN导入它会更方便。在Google上找不到任何内容?还有什么理由不存在它,或者仅仅是因为它太新了? 最佳答案 我现在不知道任何托
我试图在以下取自 Polymer Path 的上下文中理解主机和目标(和元素)和 Polymer Data Flow文档。 考虑下图: 现在考虑以下语句(来自同一个 documentation ):
polymer 文档具有以下两种方式绑定(bind)示例: Polymer({ is: 'custom-element', properties:
如果我想从具有聚合物的数组中创建纸张选择器,则看起来很麻烦: ... [[item
我最近安装了 nodejs,凉亭。然后,我使用以下命令安装了 polymer : npm install -g polymer 然后,我添加了 polymer 安装路径: C:\Users\\AppD
我正在尝试通过 querySelector 或等效方法获取在另一个元素中定义的自定义 Polymer 元素。我的代码如下: Polymer({ ready: function(){
我正在使用 Polymer v3 开发一个 Web 组件,并且需要在我的新组件的模板 HTML 中包含一些在旧式 Polymer 2 组件中定义的自定义元素。 由于 Polymer 3 不再支持 HT
我有两个 polymer 元件: 它们都是父元素内的子元素 ,并且每个都位于不同的文件中。 在 ,我有一个 ,里面有一些在那里通过点击事件来获取它的值。 目标是:在 另一方面,我想从具有如下 ur
任何人都可以向我指出使用 Polymer CLI 中的 Polymer 2 和 polymer-build 的教程吗?当我使用 polymer-starter-kit 中的任何示例并使用 polyme
我需要知道何时可以开始以编程方式使用我的自定义 Polymer 元素。元素还在undefined即使在我的 window.onload处理程序。是否有使用 Polymer 1.0 正确执行此操作的既定
我正在使用 Polymer Starter Kit 作为应用程序的基础。所以,这是一个路由到特定部分的单页应用程序。我的索引基本没变,你看这里https://github.com/PolymerEle
polymer 菜鸟... 我正在尝试根据 Polymer API 文档创建一个自定义元素,我的主页如下所示: Polymer
我在迁移到 Polymer 1.0 时遇到了一些问题 我的主要问题是“style-scope”和“my-element”类被应用于元素中的每个子节点。使用这样的东西时这不是问题:
图片中显示 People also search for 的部分,有一个可滚动的水平项目列表。我必须使用 polymer 做同样的事情,但找不到任何类似的东西。我已经实现了垂直列表,但不确定水平列表。
我想创建一个用户只能实例化一次的元素。 到目前为止,我能想到的最好的做法是在匿名函数中定义元素,并在发现该元素已存在时抛出错误。有没有办法让它拒绝被创建? (function(){ var sin
我有一个简单的元素,它只允许您一次选择一个本地文件,然后将所选文件显示为您可以删除的项目,如下所示: 该组件本身工作得很好,问题是我在同一页面中有另一个相同类型的组件,但在不同的父元素(和隐藏)内。如
我正在深入研究 Polymer 1.0 元素,我对计算的属性有点好奇。 例如,在 纸抽屉面板.html , … … … Polymer({ is: 'paper-
我是一名优秀的程序员,十分优秀!