jQuery:
var isResizing = false,
lastDownX = 0;
$(function () {
var container = $('#container'),
top = $('#top-panel'),
handle = $('#drag');
handle.on('mousedown', function (e) {
isResizing = true;
lastDownX = e.clientY;
});
$(document).on('mousemove', function (e) {
// we don't want to do anything if we aren't resizing.
if (!isResizing)
return;
var offsetRight = top.height() + (e.clientY - container.offset().top);
top.css('top', offsetRight);
}).on('mouseup', function (e) {
// stop resizing
isResizing = false;
});
});
JavaScript:
var isResizing = false
// eslint-disable-next-line no-unused-vars
var lastDownX = 0
function resizeVerticallyInit () {
var top = document.querySelector('#topology-container')
var handle = document.querySelector('#drag')
handle.addEventListener('mousedown', function (e) {
isResizing = true
lastDownX = e.clientY
})
document.addEventListener('mousemove', function (e) {
// we don't want to do anything if we aren't resizing.
if (!isResizing) {
return
}
var offsetRight = top.height() + 20
document.querySelector('#topology-container').style.top = offsetRight
})
document.addEventListener('mouseup', function (e) {
// stop resizing
isResizing = false
})
}
mounted () {
...
resizeVerticallyInit()
}
As you can see I use JavaScript function in Vue.js component in mounted() life-cycle method.
I've tried to convert it, but it doesn't work.
First of all I needed to put:
// eslint-disable-next-line no-unused-vars
var lastDownX = 0
I don't know why it's complaining that lastDownX is unused.
The second one is error:
"[Vue warn]: Error in mounted hook: "TypeError: Cannot read property 'addEventListener' of null"
It's complaining about:
handle.addEventListener('mousedown', function (e) {
isResizing = true
lastDownX = e.clientY
})
Could someone help to convert it?
resizeVerticallyInitright now. But that is probably part of the problem -$(function () {})is jQuery syntax for firing this at document.ready - if you did nothing in your implementation, to make sure the function is called after that, then it probably doesn’t find the element, because it simply does not exist yet.