简体   繁体   English

使用箭头按钮和javascript左右移动图像显示栏

[英]Moving image display bar left or right with arrow buttons and javascript

Sorry it is a small piece of code, but I am trying to scroll the image bar left or right with the two arrow buttons in my html. 对不起,这是一小段代码,但我试图用我的html中的两个箭头按钮向左或向右滚动图像栏。 The error that is showing is "Uncaught TypeError: Cannot read property 'style' of null at movingImage" Please if anyone can help, let me know. 显示的错误是“未捕获的TypeError:无法在movingImage中读取null的属性'样式”如果有人可以提供帮助,请告诉我。

JS: JS:

       function movingImage(el, e) {
            var amount = +e.target.dataset.amount;
            el.style.left = (el.offsetLeft + amount) + 'px';
        }

        var div = document.getElementById("movingImage");
        var buttons = document.querySelectorAll('button');

        [].slice.call(buttons).forEach(function (button) {
            button.onclick = movingImage.bind(this, div);
        });

HTML: HTML:

    <button data-amount="-100"><div class="arrow left small" ng-click="changeColour(index-1)" ng-src="{{colour.thumbMedia | smcmediaurl}}"></div></button>
    <button data-amount="100"><div class="arrow right small" ng-click="changeColour(index+1)" ng-src="{{colour.thumbMedia | smcmediaurl}}"></div></button>

<div id="movingImage>where images go</div>

Also Atom is saying document is undefined but not sure if that is a problem or not as not showing in console. Atom也说文档是未定义的,但不确定这是否是一个问题,因为没有在控制台中显示。

Thank you!! 谢谢!!

It's hard to tell what you're doing, because you're mixing a few different things but you tagged this question as an AngularJs question, so I made a quick example of what you would want to do with an "angular" approach. 很难说你正在做什么,因为你混合了一些不同的东西,但你把这个问题标记为AngularJs的问题,所以我做了一个快速的例子,说明你想用“角度”方法做什么。 Have a look: 看一看:

https://jsfiddle.net/26eLe0xo/ https://jsfiddle.net/26eLe0xo/

As you can see in the code below, there is a very simple controller behind the html, and it only has one function, which alters a value associated with its scope. 正如您在下面的代码中看到的,html后面有一个非常简单的控制器,它只有一个函数,它改变了与其作用域相关的值。 When the value is updated, the image's style attribute updates, moving the image 更新值后,图像的style属性会更新,移动图像

 var myApp = angular.module('myApp', []); myApp.controller('myCtrl', function($scope) { $scope.imgLeft = 100; $scope.moveImage = function(amount) { $scope.imgLeft += amount; } } ); 
 #movingImg img { position: absolute; top: 100px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" id="movingImg"> <div ng-controller="myCtrl"> <img ng-style="{'left': imgLeft+'px'}" src="http://images2.wikia.nocookie.net/__cb20110811172434/fallingskies/images/f/fd/Totoro_normal.gif" /> <button ng-click="moveImage(-10)">&lt--</button> <button ng-click="moveImage(10)">--&gt</button> </div> </div> 

Your code does not include anything that looks like AngularJs, save for the ng-click tags in your html buttons, so I would recommend not mixing too much vanilla js with Angular. 你的代码不包含任何看起来像AngularJs的东西,除了你的html按钮中的ng-click标签,所以我建议不要与Angular混合过多的vanilla js。

In any case, document will be a global variable in your browser, so you're okay with that, it's most likely a warning in Atom 在任何情况下, document都将是浏览器中的全局变量,因此您可以使用它,它很可能是Atom中的警告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM