杨辰沁再不疯狂就老了
2017-04-06 20:45
/** * Created by ycx on 2017/4/6. */ ;(function () { 'use strict'; var $oDiv = $('.block'); console.log($oDiv); $oDiv.onmouseover = function () { startMove(0); } $oDiv.onmouseout = function () { startMove(-200); } var timer = null; function startMove(target) { clearInterval(timer); var $oDiv = $('.block'); var speed = 0; if ($oDiv.offsetLeft > target) { speed = -10; } else { speed = 10; } timer = setInterval(function () { if ($oDiv.offsetLeft == target) { clearInterval(timer); } else { $oDiv.style.left = $oDiv.offsetLeft + speed + 'px'; } }, 30) } })();
在选择器var $oDiv = $('.block');这里就报错没有$()这个方法,怀疑是引用的问题,但是不管是npm安装jquery,还是直接引用cdn,都不行,到底是什么原因啊??
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <script src="base.js"></script>
<script src="node_modules/jquery/dist/jquery.js"></script> <script src="base.js"></script>
1、事件写错了,jq的事件绑定是$('...').mouseover(function () {。。。})
2、
开头的这个分号跟结尾的括号啥意思?
jq的onload方法不是应该这样写——
$(document).ready(function() {
// 。。。
});
或者——
$(function() {
//。。。
});
$oDiv .on('mouseover',function(){
startMove(-200);
})
JS动画效果
113920 学习 · 1502 问题
相似问题